使用平均值填充 pandas 数据帧中的缺失值 [英] Fill in missing values in pandas dataframe using mean
本文介绍了使用平均值填充 pandas 数据帧中的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
datetime
2012-01-01 125.5010
2012-01-02 NaN
2012-01-03 125.5010
2013-01-04 NaN
2013-01-05 125.5010
2013-02-28 125.5010
2014-02-28 125.5010
2016-01-02 125.5010
2016-01-04 125.5010
2016-02-28 NaN
我想填写missig通过使用从数据集计算出的气候学来计算此数据框中的值,即通过平均值$ code> 28th feb $ c $填写缺少 28th feb 2016
c>其他年份。我如何做到这一点?
I would like to fill in the missig values in this dataframe by using a climatology computed from the dataset i.e fill in missing 28th feb 2016
value by averaging values of 28th feb
from other years. How do i do this?
推荐答案
你可以使用 groupby
by month
和 day
和 转换
与 fillna
意味着
:
You can use groupby
by month
and day
and transform
with fillna
mean
:
print df.groupby([df.index.month, df.index.day]).transform(lambda x: x.fillna(x.mean()))
datetime
2012-01-01 125.501
2012-01-02 125.501
2012-01-03 125.501
2013-01-04 125.501
2013-01-05 125.501
2013-02-28 125.501
2014-02-28 125.501
2016-01-02 125.501
2016-01-04 125.501
2016-02-28 125.501
这篇关于使用平均值填充 pandas 数据帧中的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文