Calculate average of every x rows in a table and create new table

You can create an artificial group using df.index//2 (or as @DSM pointed out, using np.arange(len(df))//2 – so that it works for all indices) and then use groupby:

df.groupby(np.arange(len(df))//2).mean()
Out[13]: 
      a     b     c     d
0   3.0  30.5  31.5  35.0
1   7.0  35.0  21.5  25.0
2  11.0  37.5  41.5  38.5
3  15.0  10.0  16.0  18.5
4  19.0  15.5  27.0  38.0

Leave a Comment