在Pandas中将float64列转换为int64 [英] Convert float64 column to int64 in Pandas
问题描述
我尝试使用以下方式将列从数据类型float64
转换为int64
:
I tried to convert a column from data type float64
to int64
using:
df['column name'].astype(int64)
但出现错误:
NameError:未定义名称'int64'
NameError: name 'int64' is not defined
该列有人数,但其格式为7500000.0
,您知道如何将float64
更改为int64
吗?
The column has number of people but was formatted as 7500000.0
, any idea how I can simply change this float64
into int64
?
推荐答案
大熊猫 0.24 + 的解决方案,用于转换具有缺失值的数字:
Solution for pandas 0.24+ for converting numeric with missing values:
df = pd.DataFrame({'column name':[7500000.0,7500000.0, np.nan]})
print (df['column name'])
0 7500000.0
1 7500000.0
2 NaN
Name: column name, dtype: float64
df['column name'] = df['column name'].astype(np.int64)
ValueError:无法将非有限值(NA或inf)转换为整数
ValueError: Cannot convert non-finite values (NA or inf) to integer
#http://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html
df['column name'] = df['column name'].astype('Int64')
print (df['column name'])
0 7500000
1 7500000
2 NaN
Name: column name, dtype: Int64
我认为您需要转换为 numpy.int64
:
I think you need cast to numpy.int64
:
df['column name'].astype(np.int64)
示例:
df = pd.DataFrame({'column name':[7500000.0,7500000.0]})
print (df['column name'])
0 7500000.0
1 7500000.0
Name: column name, dtype: float64
df['column name'] = df['column name'].astype(np.int64)
#same as
#df['column name'] = df['column name'].astype(pd.np.int64)
print (df['column name'])
0 7500000
1 7500000
Name: column name, dtype: int64
如果列中的某些NaN
需要用0
) .Series.fillna.html"rel =" noreferrer> fillna
,因为NaN
中的type
是float
:
If some NaN
s in columns need replace them to some int
(e.g. 0
) by fillna
, because type
of NaN
is float
:
df = pd.DataFrame({'column name':[7500000.0,np.nan]})
df['column name'] = df['column name'].fillna(0).astype(np.int64)
print (df['column name'])
0 7500000
1 0
Name: column name, dtype: int64
还要检查文档-缺少数据投射规则
用NaN
s转换值是错误的:
Convert values with NaN
s is buggy:
df = pd.DataFrame({'column name':[7500000.0,np.nan]})
df['column name'] = df['column name'].values.astype(np.int64)
print (df['column name'])
0 7500000
1 -9223372036854775808
Name: column name, dtype: int64
这篇关于在Pandas中将float64列转换为int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!