简体   繁体   English

使用均值填写熊猫数据框中的缺失值

[英]Fill in missing values in pandas dataframe using mean

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

I would like to fill in the missig values in this dataframe by using a climatology computed from the dataset ie fill in missing 28th feb 2016 value by averaging values of 28th feb from other years. 我想用从数据集,即计算的气候填写missig值这个数据帧填补缺失的28th feb 2016通过平均的值价值28th feb从其他年份。 How do i do this? 我该怎么做呢?

You can use groupby by month and day and transform with fillna mean : 您可以monthday使用groupby并使用fillna mean transform

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM