如何舍入/删除除法".0&" pandas 列中的零? [英] how to round/remove traling ".0" zeros in pandas column?

查看:72
本文介绍了如何舍入/删除除法".0&" pandas 列中的零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看是否可以从此电话号码栏中删除结尾的零.

I'm trying to see if I can remove the trailing zeros from this phone number column.

示例:

0
1      8.00735e+09
2      4.35789e+09
3      6.10644e+09

此列中的类型是一个对象,我试图将其取整,但出现错误.我检查了其中的几个,我知道它们的格式为"8007354384.0",并希望除去带小数点的尾随零.

The type in this column is an object, and I tried to round it but I am getting an error. I checked a couple of them I know they are in this format "8007354384.0", and want to get rid of the trailing zeros with the decimal point.

有时我会收到这种格式,有时却没有,它们将是整数.我想检查一下phone列中是否有尾随零,然后将其删除.

Sometimes I received in this format and sometimes I don't, they will be integer numbers. I would like to check if the phone column has a trailing zero, then remove it.

我有这段代码,但是我对如何检查每一行的尾随零感到困惑.

I have this code but I'm stuck on how to check for trailing zeros for each row.

data.ix[data.phone.str.contains('.0'), 'phone']

我得到一个错误=> *** ValueError: cannot index with vector containing NA / NaN values.我认为问题是因为某些行有空数据,有时我确实收到了.上面的代码应该能够跳过空行.

I get an error => *** ValueError: cannot index with vector containing NA / NaN values. I believe the issue is because some rows have empty data, which sometime I do receive. The code above should be able to skip an empty row.

有人有什么建议吗?我是熊猫的新手,但到目前为止它是一个有用的库.您的帮助将不胜感激.

Does anybody have any suggestions? I'm new to pandas but so far it's an useful library. Your help will be appreciated.

注意 上面提供的示例中,第一行包含一个空数据,有时我会这样做.只是为了确保此电话号码不被表示为0.

Note The provided example above, the first row has an empty data, which I do sometimes I get. Just to make sure this is not represented as 0 for phone number.

空数据也被视为字符串,因此如果行为空,则它是浮点数和字符串的混合.

Also empty data is considered a string, so it's a mix of floats and string, if rows are empty.

推荐答案

使用astype(np.int64)

s = pd.Series(['', 8.00735e+09, 4.35789e+09, 6.10644e+09])
mask = pd.to_numeric(s).notnull()
s.loc[mask] = s.loc[mask].astype(np.int64)
s

0              
1    8007350000
2    4357890000
3    6106440000
dtype: object

这篇关于如何舍入/删除除法".0&" pandas 列中的零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆