Fill in missing values in pandas dataframe using mean

做~自己de王妃 提交于 2019-12-11 14:08:51

问题


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 i.e fill in missing 28th feb 2016 value by averaging values of 28th feb from other years. How do i do this?


回答1:


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


来源:https://stackoverflow.com/questions/34972297/fill-in-missing-values-in-pandas-dataframe-using-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!