pandas DataFrame:用列的平均值替换nan值 [英] pandas DataFrame: replace nan values with average of columns

查看:927
本文介绍了pandas DataFrame:用列的平均值替换nan值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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 nans 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屋!

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