问题描述
我有一个如下数据框
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属性我尝试了熊猫0.9的.sparse数据属性我也尝试过NaN == NaN语句在函数中.我也看过这篇文章如何替换NA 在R数据帧中是否具有零值?,同时查看其他一些文章.我尝试过的所有方法均无效或无法识别NaN.任何提示或解决方案将不胜感激.
I have tried applying a function using .isnan from the Math ModuleI have tried the pandas .replace attributeI tried the .sparse data attribute from pandas 0.9I 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
这篇关于如何在 pandas 数据框的列中将所有NaN值替换为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!