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值,即通过平均来自其他年份的28th feb 2016
值来填充缺失的28th feb
值。我该怎么做?
答案 0 :(得分:1)
您可以使用month
day
和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
以及groupby
transform
fillna
使用mean
:
{{1}}