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 array:用列的平均值替换 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.
推荐答案
您可以简单地使用 DataFrame.fillna
直接填充nan
:
In [27]: df
Out[27]:
A B C
0 -0.166919 0.979728 -0.632955
1 -0.297953 -0.912674 -1.365463
2 -0.120211 -0.540679 -0.680481
3 NaN -2.027325 1.533582
4 NaN NaN 0.461821
5 -0.788073 NaN NaN
6 -0.916080 -0.612343 NaN
7 -0.887858 1.033826 NaN
8 1.948430 1.025011 -2.982224
9 0.019698 -0.795876 -0.046431
In [28]: df.mean()
Out[28]:
A -0.151121
B -0.231291
C -0.530307
dtype: float64
In [29]: df.fillna(df.mean())
Out[29]:
A B C
0 -0.166919 0.979728 -0.632955
1 -0.297953 -0.912674 -1.365463
2 -0.120211 -0.540679 -0.680481
3 -0.151121 -2.027325 1.533582
4 -0.151121 -0.231291 0.461821
5 -0.788073 -0.231291 -0.530307
6 -0.916080 -0.612343 -0.530307
7 -0.887858 1.033826 -0.530307
8 1.948430 1.025011 -2.982224
9 0.019698 -0.795876 -0.046431
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屋!