pd.to_numeric将整个系列转换为NaN

时间:2018-01-05 23:26:05

标签: python pandas dataframe

我尝试使用pd.to_numeric转换列,但由于某种原因,它将所有值(除了一个)转换为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

关于为什么会这样的想法?原始数据如下所示:

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 个答案:

答案 0 :(得分:5)

选项1
阅读CSV时,请使用thousands参数 -

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

选项2
如果选项1 不起作用,那么您需要先使用str.replace删除逗号,然后然后调用{ {1}}。

pd.to_numeric