I want to select top two rows every 5 rows in pandas dataframe. How could I do this?
Consider the following dataframe:
col1 | col2 | col3
1 | 1 | 1
2. | 2. | 2
3. | 3. | 3
4 | 4 | 4
5. | 5. | 5
6. | 6. | 6
7. | 7. | 7
I would like to get the following values
col1 | col2 | col3
1 | 1 | 1
2. | 2. | 2
and
col1 | col2 | col3
6. | 6. | 6
7. | 7. | 7
I will really appreciate the help!!!
Use floor division on the index and groupby on it with head:
df.groupby(df.index//5).head(2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With