如何在 Pandas 数据帧的列中用零替换 NaN 值? [英] How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

查看:47
本文介绍了如何在 Pandas 数据帧的列中用零替换 NaN 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Pandas 数据框,如下所示:

I have a Pandas Dataframe as below:

      itm Date                  Amount 
67    420 2012-09-30 00:00:00   65211
68    421 2012-09-09 00:00:00   29424
69    421 2012-09-16 00:00:00   29877
70    421 2012-09-23 00:00:00   30990
71    421 2012-09-30 00:00:00   61303
72    485 2012-09-09 00:00:00   71781
73    485 2012-09-16 00:00:00     NaN
74    485 2012-09-23 00:00:00   11072
75    485 2012-09-30 00:00:00  113702
76    489 2012-09-09 00:00:00   64731
77    489 2012-09-16 00:00:00     NaN

当我尝试将函数应用于金额列时,出现以下错误:

When I try to apply a function to the Amount column, I get the following error:

ValueError: cannot convert float NaN to integer

我尝试使用数学模块中的 .isnan 应用函数我已经尝试过熊猫的 .replace 属性我尝试了 pandas 0.9 的 .sparse data 属性我也试过函数中的 if NaN == NaN 语句.我也看过这篇文章 How do I replace NAR 数据框中的值为零? 同时查看其他一些文章.我尝试过的所有方法都不起作用或无法识别 NaN.任何提示或解决方案将不胜感激.

I have tried applying a function using .isnan from the Math Module I have tried the pandas .replace attribute I tried the .sparse data attribute from pandas 0.9 I have also tried if NaN == NaN statement in a function. I have also looked at this article How do I replace NA values with zeros in an R dataframe? whilst looking at some other articles. All the methods I have tried have not worked or do not recognise NaN. Any Hints or solutions would be appreciated.

推荐答案

我相信 DataFrame.fillna() 会为你做到这一点.

I believe DataFrame.fillna() will do this for you.

数据框的文档链接以及系列.

示例:

In [7]: df
Out[7]: 
          0         1
0       NaN       NaN
1 -0.494375  0.570994
2       NaN       NaN
3  1.876360 -0.229738
4       NaN       NaN

In [8]: df.fillna(0)
Out[8]: 
          0         1
0  0.000000  0.000000
1 -0.494375  0.570994
2  0.000000  0.000000
3  1.876360 -0.229738
4  0.000000  0.000000

要仅在一列中填充 NaN,请仅选择该列.在这种情况下,我使用 inplace=True 来实际更改 df 的内容.

To fill the NaNs in only one column, select just that column. in this case I'm using inplace=True to actually change the contents of df.

In [12]: df[1].fillna(0, inplace=True)
Out[12]: 
0    0.000000
1    0.570994
2    0.000000
3   -0.229738
4    0.000000
Name: 1

In [13]: df
Out[13]: 
          0         1
0       NaN  0.000000
1 -0.494375  0.570994
2       NaN  0.000000
3  1.876360 -0.229738
4       NaN  0.000000

为了避免 SettingWithCopyWarning,请使用内置的特定于列的功能:

To avoid a SettingWithCopyWarning, use the built in column-specific functionality:

df.fillna({1:0}, inplace=True)

这篇关于如何在 Pandas 数据帧的列中用零替换 NaN 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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