pd.to_numeric将整个序列转换为NaN [英] pd.to_numeric converts entire series to NaN

查看:61
本文介绍了pd.to_numeric将整个序列转换为NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pd.to_numeric转换列,但由于某种原因,它将所有值(一个值除外)都转换为NaN:

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

推荐答案

read_csvthousands=','

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

这可以解决读取数据时的问题.

This fixes the problem while reading your data.

df['Principal Remaining'] = pd.to_numeric(
    df['Principal Remaining'].str.replace(',', ''), errors='coerce')

如果没有选择第一个选项,则需要先使用str.replace消除逗号,然后然后调用pd.to_numeric,如下所示.

If the first option isn't a choice, you'll need to get rid of the commas first using str.replace, then call pd.to_numeric as shown here.

这篇关于pd.to_numeric将整个序列转换为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆