Skip to content Skip to sidebar Skip to footer

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.50

Solution 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

Post a Comment for "Fill In Missing Values In Pandas Dataframe Using Mean"