Bin pandas dataframe by every X rows
Asked Answered
D

3

28

I have a simple dataframe which I would like to bin for every 3 rows.

It looks like this:

    col1
0      2
1      1
2      3
3      1
4      0

and I would like to turn it into this:

    col1
0      2
1    0.5

I have already posted a similar question here but I have no Idea how to port the solution to my current use case.

Can you help me out?

Many thanks!

Decastro answered 24/11, 2013 at 20:6 Comment(0)
L
53

In Python 2 use:

>>> df.groupby(df.index / 3).mean()
   col1
0   2.0
1   0.5
Lewiss answered 24/11, 2013 at 20:9 Comment(4)
such a simple and elegant solution!Frydman
I get 0.000000 2, 0.333333 1, 0.666667 3, 1.000000 1, 1.333333 0 with the latest Python and Pandas version. Probably has to do with integer division. Edit: Yes, Python 3 users, use df.index // 3Spada
Is there an equivalent way to do this if your dataframe has a datetime index, and you were insisting on doing every n rows?Vague
@Seth: You could reset the index. Not sure if you want to use every nth row. If so, use modulo (%) instead.Blackfellow
S
29

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
Sielen answered 8/6, 2018 at 8:26 Comment(0)
M
4

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()
Mentality answered 27/8, 2018 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.