Bin pandas dataframe by every X rows Bin pandas dataframe by every X rows python python

Bin pandas dataframe by every X rows


In Python 2 use:

>>> df.groupby(df.index / 3).mean()   col10   2.01   0.5


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()   col10   2.01   0.5


For Python 2 (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()