我有一个数据集,其中 pandas.read_csv() 处理适当地将一些连续数字列/特征/变量数据从对象转换为 float64 [ 、 int64 或 uint8 ] 但不是其他数据。
因此,然后我尝试使用以下指定向下转换参数的 pandas.to_numeric() 调用来转换应该转换为连续数字类型(特别是 int64)的列数据,但我仍然得到 float64 结果。
df.wc = pd.to_numeric(df.wc, errors='coerce', downcast='signed')
# call to convert object to int64 vs float64
尝试将对象类型转换为最具体的连续数字类型时,是否存在典型的列/功能/变量集问题会导致参数设置被忽略?
根据文档 http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html
...将结果数据向下转换为最小的数字数据类型possible根据 ...
根据我的实验,可以向下转换为整数值,例如
pd.to_numeric(pd.Series([1.0, 2.0]), downcast='unsigned')
0 1
1 2
dtype: uint8
不过,不可能向下转换为整数值,例如
pd.to_numeric(pd.Series([1.1, 2.1]), downcast='unsigned')
0 1.1
1 2.1
dtype: float64
如果你想在结果中获取int64值,那么你可以应用pd.Series.astype https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html
pd.Series([1.1, 2.1]).astype(int)
0 1
1 2
dtype: int64
您可能感兴趣
- 在Python中何时应用(pd.to_numeric)以及何时应用astype(np.float64)? https://stackoverflow.com/questions/40095712/when-to-applypd-to-numeric-and-when-to-astypenp-float64-in-python
- pd.to_numeric(series, downcast='integer') 不能正确处理超过 10,000 的浮点数 https://github.com/pandas-dev/pandas/issues/14941
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)