我一直在处理从CSV导入的数据。Pandas将一些列更改为浮点数,所以现在这些列中的数字显示为浮点数!但是,我需要将它们显示为整数或不带逗号。是否有方法将它们转换为整数或不显示逗号?
当前回答
在问题的文本中解释了数据来自csv。Só,我认为显示选项,使转换时,数据读取,而不是之后,是相关的主题。
当在数据框架中导入电子表格或csv时,“只有整数列”通常会转换为浮点数,因为excel将所有数值存储为浮点数,以及底层库的工作方式。
当使用read_excel或read_csv读取文件时,有几个选项可以避免导入后转换:
参数dtype允许传递一个包含列名和目标类型的字典,例如dtype = {"my_column": "Int64"} 参数转换器可以用来传递进行转换的函数,例如用0改变NaN。转换= {"my_column": lambda x: int(x) if x else 0} parameter convert_float将“整型浮点数转换为int(即1.0 - > 1)”,但要注意像NaN这样的极端情况。该参数仅在read_excel中有效
要在现有的数据帧中进行转换,其他注释中已经给出了几种替代方法,但由于v1.0.0 pandas有一个有趣的函数:convert_dtypes,即“使用支持pd.NA的dtypes将列转换为最佳的dtypes”。
为例:
In [3]: import numpy as np
In [4]: import pandas as pd
In [5]: df = pd.DataFrame(
...: {
...: "a": pd.Series([1, 2, 3], dtype=np.dtype("int64")),
...: "b": pd.Series([1.0, 2.0, 3.0], dtype=np.dtype("float")),
...: "c": pd.Series([1.0, np.nan, 3.0]),
...: "d": pd.Series([1, np.nan, 3]),
...: }
...: )
In [6]: df
Out[6]:
a b c d
0 1 1.0 1.0 1.0
1 2 2.0 NaN NaN
2 3 3.0 3.0 3.0
In [7]: df.dtypes
Out[7]:
a int64
b float64
c float64
d float64
dtype: object
In [8]: converted = df.convert_dtypes()
In [9]: converted.dtypes
Out[9]:
a Int64
b Int64
c Int64
d Int64
dtype: object
In [10]: converted
Out[10]:
a b c d
0 1 1 1 1
1 2 2 <NA> <NA>
2 3 3 3 3
其他回答
使用pandas. datafframe .astype(<type>)函数来操作列的dtypes。
>>> df = pd.DataFrame(np.random.rand(3,4), columns=list("ABCD"))
>>> df
A B C D
0 0.542447 0.949988 0.669239 0.879887
1 0.068542 0.757775 0.891903 0.384542
2 0.021274 0.587504 0.180426 0.574300
>>> df[list("ABCD")] = df[list("ABCD")].astype(int)
>>> df
A B C D
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
编辑:
处理缺失值:
>>> df
A B C D
0 0.475103 0.355453 0.66 0.869336
1 0.260395 0.200287 NaN 0.617024
2 0.517692 0.735613 0.18 0.657106
>>> df[list("ABCD")] = df[list("ABCD")].fillna(0.0).astype(int)
>>> df
A B C D
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
需要转换为int的列也可以在字典中提到,如下所示
df = df.astype({'col1': 'int', 'col2': 'int', 'col3': 'int'})
虽然这里有很多选择, 还可以使用字典转换特定列的格式
Data = pd.read_csv('Your_Data.csv')
Data_2 = Data.astype({"Column a":"int32", "Column_b": "float64", "Column_c": "int32"})
print(Data_2 .dtypes) # Check the dtypes of the columns
这是更改特定列的数据格式以进行快速数据分析的一种有用且非常快速的方法。
考虑以下数据帧:
>>> df = pd.DataFrame(10*np.random.rand(3, 4), columns=list("ABCD"))
>>> print(df)
... A B C D
... 0 8.362940 0.354027 1.916283 6.226750
... 1 1.988232 9.003545 9.277504 8.522808
... 2 1.141432 4.935593 2.700118 7.739108
使用列名列表,使用applymap()更改多个列的类型:
>>> cols = ['A', 'B']
>>> df[cols] = df[cols].applymap(np.int64)
>>> print(df)
... A B C D
... 0 8 0 1.916283 6.226750
... 1 1 9 9.277504 8.522808
... 2 1 4 2.700118 7.739108
或者对于单个列使用apply():
>>> df['C'] = df['C'].apply(np.int64)
>>> print(df)
... A B C D
... 0 8 0 1 6.226750
... 1 1 9 9 8.522808
... 2 1 4 2 7.739108
>>> 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
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用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中获取迭代器中的元素个数
- 解析日期字符串并更改格式