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

查看:94
本文介绍了如何在 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

当我尝试将函数应用于Amount列,我得到以下错误:

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

ValueError: cannot convert float NaN to integer

我尝试使用数学模块
中的.isnan应用函数。我尝试了pandas .replace属性
。来自pandas 0.9
的稀疏数据属性我也尝试过如果NaN == NaN语句功能。
我也看过这篇文章如何在查看其他文章时,如何在R数据帧中用零替换NA值?
我尝试过的所有方法均无效或无法识别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天全站免登陆