Bin pandas dataframe by every X rows

╄→尐↘猪︶ㄣ 提交于 2019-11-27 22:58:16
>>> df.groupby(df.index / 3).mean()
   col1
0   2.0
1   0.5
ojunk

The answer from Roman Pekar was not working for me. I imagine that this is because of differences between Python2 and Python3. This worked for me in Python3:

>>> df.groupby(df.index // 3).mean()
   col1
0   2.0
1   0.5

For Python 2.2+ users, who have "true division" enabled (e.g. by using from __future__ import division), you need to use the "//" operator for "floor division":

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