pandas DataFrame:用列的平均值替换nan值 [英] pandas DataFrame: replace nan values with average of columns
问题描述
我的pandas DataFrame主要填充了实数,但是其中也有一些nan
值.
I've got a pandas DataFrame filled mostly with real numbers, but there is a few nan
values in it as well.
如何用列的平均值代替nan
?
How can I replace the nan
s with averages of columns where they are?
此问题与以下问题非常相似: numpy数组:用列的平均值替换nan值,但是不幸的是,给出的解决方案不适用于pandas DataFrame.
This question is very similar to this one: numpy array: replace nan values with average of columns but, unfortunately, the solution given there doesn't work for a pandas DataFrame.
推荐答案
您可以简单地使用 fillna
的文档字符串说value
应该是标量或字典,但是,它似乎也可以与Series
一起使用.如果您想传递命令,可以使用df.mean().to_dict()
.
The docstring of fillna
says that value
should be a scalar or a dict, however, it seems to work with a Series
as well. If you want to pass a dict, you could use df.mean().to_dict()
.
这篇关于pandas DataFrame:用列的平均值替换nan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!