我一直在处理从CSV导入的数据。Pandas将一些列更改为浮点数,所以现在这些列中的数字显示为浮点数!但是,我需要将它们显示为整数或不带逗号。是否有方法将它们转换为整数或不显示逗号?
当前回答
使用'Int64'支持NaN
Astype (int)和Astype ('int64')不能处理缺失值(numpy int) astype('Int64')(注意大写I)可以处理缺失值(pandas int)
df['A'] = df['A'].astype('Int64') # capital I
这假设您希望将缺失的值保留为NaN。如果你打算归因他们,你可以按照Ryan的建议先填写na。
'Int64'(大写I)的例子
If the floats are already rounded, just use astype: df = pd.DataFrame({'A': [99.0, np.nan, 42.0]}) df['A'] = df['A'].astype('Int64') # A # 0 99 # 1 <NA> # 2 42 If the floats are not rounded yet, round before astype: df = pd.DataFrame({'A': [3.14159, np.nan, 1.61803]}) df['A'] = df['A'].round().astype('Int64') # A # 0 3 # 1 <NA> # 2 2 To read int+NaN data from a file, use dtype='Int64' to avoid the need for converting at all: csv = io.StringIO(''' id,rating foo,5 bar, baz,2 ''') df = pd.read_csv(csv, dtype={'rating': 'Int64'}) # id rating # 0 foo 5 # 1 bar <NA> # 2 baz 2
笔记
'Int64'是Int64Dtype的别名: df['A'] = df['A'].astype(pd.Int64Dtype()) #与astype('Int64')相同 大小/签名别名可用: 下界 上界 “Int8” -128年 127 “Int16” -32768年 32767年 “Int32” -2147483648年 2147483647年 “Int64” -9223372036854775808年 9223372036854775807年 “UInt8” 0 255 “UInt16” 0 65535年 “UInt32” 0 4294967295年 “UInt64” 0 18446744073709551615年
其他回答
需要转换为int的列也可以在字典中提到,如下所示
df = df.astype({'col1': 'int', 'col2': 'int', 'col3': 'int'})
这是一个快速的解决方案,如果你想转换更多的列的熊猫。DataFrame从浮点数到整数也考虑到你可以有NaN值的情况。
cols = ['col_1', 'col_2', 'col_3', 'col_4']
for col in cols:
df[col] = df[col].apply(lambda x: int(x) if x == x else "")
我尝试用else x)和else None),但结果仍然有浮点数,所以我使用else ""。
使用'Int64'支持NaN
Astype (int)和Astype ('int64')不能处理缺失值(numpy int) astype('Int64')(注意大写I)可以处理缺失值(pandas int)
df['A'] = df['A'].astype('Int64') # capital I
这假设您希望将缺失的值保留为NaN。如果你打算归因他们,你可以按照Ryan的建议先填写na。
'Int64'(大写I)的例子
If the floats are already rounded, just use astype: df = pd.DataFrame({'A': [99.0, np.nan, 42.0]}) df['A'] = df['A'].astype('Int64') # A # 0 99 # 1 <NA> # 2 42 If the floats are not rounded yet, round before astype: df = pd.DataFrame({'A': [3.14159, np.nan, 1.61803]}) df['A'] = df['A'].round().astype('Int64') # A # 0 3 # 1 <NA> # 2 2 To read int+NaN data from a file, use dtype='Int64' to avoid the need for converting at all: csv = io.StringIO(''' id,rating foo,5 bar, baz,2 ''') df = pd.read_csv(csv, dtype={'rating': 'Int64'}) # id rating # 0 foo 5 # 1 bar <NA> # 2 baz 2
笔记
'Int64'是Int64Dtype的别名: df['A'] = df['A'].astype(pd.Int64Dtype()) #与astype('Int64')相同 大小/签名别名可用: 下界 上界 “Int8” -128年 127 “Int16” -32768年 32767年 “Int32” -2147483648年 2147483647年 “Int64” -9223372036854775808年 9223372036854775807年 “UInt8” 0 255 “UInt16” 0 65535年 “UInt32” 0 4294967295年 “UInt64” 0 18446744073709551615年
>>> import pandas as pd
>>> right = pd.DataFrame({'C': [1.002, 2.003], 'D': [1.009, 4.55], 'key': ['K0', 'K1']})
>>> print(right)
C D key
0 1.002 1.009 K0
1 2.003 4.550 K1
>>> right['C'] = right.C.astype(int)
>>> print(right)
C D key
0 1 1.009 K0
1 2 4.550 K1
将所有浮点列转换为int
>>> df = pd.DataFrame(np.random.rand(5, 4) * 10, columns=list('PQRS'))
>>> print(df)
... P Q R S
... 0 4.395994 0.844292 8.543430 1.933934
... 1 0.311974 9.519054 6.171577 3.859993
... 2 2.056797 0.836150 5.270513 3.224497
... 3 3.919300 8.562298 6.852941 1.415992
... 4 9.958550 9.013425 8.703142 3.588733
>>> float_col = df.select_dtypes(include=['float64']) # This will select float columns only
>>> # list(float_col.columns.values)
>>> for col in float_col.columns.values:
... df[col] = df[col].astype('int64')
>>> print(df)
... P Q R S
... 0 4 0 8 1
... 1 0 9 6 3
... 2 2 0 5 3
... 3 3 8 6 1
... 4 9 9 8 3
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 确定每列中NA值的个数
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 如何结合多个条件子集数据帧使用“或”?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式