pandas calculating mean per month

允我心安 提交于 2019-12-11 07:00:32

问题


I created the following dataframe:

availability = pd.DataFrame(propertyAvailableData).set_index("createdat")

monthly_availability = availability.fillna(value=0).groupby(pd.TimeGrouper(freq='M'))

This gives the following output

            2015-08-18  2015-09-09  2015-09-10  2015-09-11  2015-09-12  \
createdat                                                                
2015-08-12         1.0         1.0         1.0         1.0         1.0   
2015-08-17         0.0         0.0         0.0         0.0         0.0   
2015-08-18         0.0         1.0         1.0         1.0         1.0   
2015-08-18         0.0         0.0         0.0         0.0         0.0   
2015-08-19         0.0         1.0         1.0         1.0         1.0   
2015-09-03         0.0         1.0         1.0         1.0         1.0   
2015-09-03         0.0         1.0         1.0         1.0         1.0   
2015-09-07         0.0         0.0         0.0         0.0         0.0   
2015-09-08         0.0         0.0         0.0         0.0         0.0   
2015-09-11         0.0         0.0         0.0         0.0         0.0   

I'm trying to get the averages per created at month by doing:

monthly_availability_mean = monthly_availability.mean()

However, here I get the following output:

            2015-08-18  2015-09-09  2015-09-10  2015-09-11  2015-09-12  \
createdat                                                                
2015-08-31    0.111111    0.444444    0.666667    0.777778    0.777778   
2015-09-30    0.000000    0.222222    0.222222    0.222222    0.222222   
2015-10-31    0.000000    0.000000    0.000000    0.000000    0.000000   

And when I hand check august I get:

1.0 + 0 + 0 + 0 + 0 / 5 = 0.2

How do I get the correct mean per month?


回答1:


availability.resample('M').mean()


来源:https://stackoverflow.com/questions/42388707/pandas-calculating-mean-per-month

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