pd.to_numeric converts entire series to NaN [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:42:02

问题:

This question already has an answer here:

I'm trying to convert a column using pd.to_numeric, but for some reason it turns all values (except one) into NaN:

In[]: pd.to_numeric(portfolio["Principal Remaining"],errors="coerce") Out[]:  1           NaN 2           NaN 3           NaN 4           NaN 5           NaN 6           NaN 7           NaN 8           NaN 9           NaN 10          NaN 11          NaN 12          NaN 13          NaN 14          NaN 15          NaN 16          NaN 17          NaN 18       836.61 19          NaN 20          NaN       ...   Name: Principal Remaining, Length: 32314, dtype: float64 

Thoughts on why this is happening? The original data looks like this:

1         18,052.02 2         27,759.85 3         54,061.75 4         89,363.61 5         46,954.46 6         64,295.64 7        100,000.00 8         27,905.98 9         13,821.48 10        16,937.89         ...     Name: Principal Remaining, Length: 32314, dtype: object 

回答1:

Option 1
When reading your CSV, use the thousands argument -

df = pd.read_csv('file.csv', thousands=',') 

Option 2
If Option 1 doesn't work, then you'll need to get rid of the commas first, using str.replace, and then call pd.to_numeric.

pd.to_numeric(df['Principal Remaining'].str.replace(',', ''), errors='coerce')  1      18052.02 2      27759.85 3      54061.75 4      89363.61 5      46954.46 6      64295.64 7     100000.00 8      27905.98 9      13821.48 10     16937.89 Name: Principal Remaining, dtype: float64 


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