pandas convert objects with numbers and nans to ints or floats

a 夏天 提交于 2020-05-15 02:40:30

问题


with the knowledge that similar cases have been answered several times I couldn't make it work anyway.

sample data:

10
5
20

5

6

after i figured out that with:

df = df['column_name'].astype(str).astype(int)

it would work if there wasn't nans in the input data.

error: invalid literal for int() with base 10: 'nan'

Also I did try to use float instead but it gives an error as well

error: could not convert string to float

what am I missing?

output can be anything with "null", "nan", "" for example:

10
5
20
null
5
null
6

回答1:


You can convert to numeric with to_numeric and errors='coerce' for floats in columns and for integers use nullable integer data type (pandas 0.24+):

df['column_name'] = pd.to_numeric(df['column_name'], errors='coerce').astype('Int64')
print (df)
   column_name
0           10
1            5
2           20
3          NaN
4            5
5          NaN
6            6

Detail:

print (pd.to_numeric(df['column_name'], errors='coerce'))
0    10.0
1     5.0
2    20.0
3     NaN
4     5.0
5     NaN
6     6.0
Name: column_name, dtype: float64


来源:https://stackoverflow.com/questions/57656860/pandas-convert-objects-with-numbers-and-nans-to-ints-or-floats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!