在Pandas中将float64列转换为int64
我试图将列从数据类型转换float64
为int64
使用:
df['column name'].astype(int64)
但出现错误:
NameError:未定义名称“ int64”
专栏有人数,但其格式为:7500000.0
我知道如何将其更改float64
为int64
?
回答:
大熊猫 解决方案,用于转换缺少值的数字:
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)转换为整数
#http://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.htmldf['column name'] = df['column name'].astype('Int64')
print (df['column name'])
0 7500000
1 7500000
2 NaN
Name: column name, dtype: Int64
我认为您需要转换为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
S
IN列需要他们取代一些int
(例如0
)通过fillna
,因为type
的NaN
是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转换值是错误的:
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 的全部内容, 来源链接: utcz.com/qa/432119.html