【发布时间】:2020-10-26 21:57:18
【问题描述】:
df['Value'] = [-2,254.00, 3,452.00, 15,698.00, -6,258.00]
数据类型是对象。我想将它们转换为整数,但出现错误
【问题讨论】:
df['Value'] = [-2,254.00, 3,452.00, 15,698.00, -6,258.00]
数据类型是对象。我想将它们转换为整数,但出现错误
【问题讨论】:
使用df.astype:
In [2329]: df['Value'] = df['Value'].astype(int)
In [2329]: df['Value']
Out[2329]:
0 -2
1 254
2 3
3 452
4 15
5 698
6 -6
7 258
Name: Value, dtype: int64
【讨论】:
df['Value'] = df['Value'].astype(int)这是我的命令。
accept它。
你可以试试下面的代码:
df['Value'] = df['Value'].astype(dtype='int64')
【讨论】:
将pd.to_numeric 与errors='coerce' 一起使用
df['Value'] = pd.to_numeric(df['Value'], errors='coerce')
【讨论】: