Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas groupby filter only last two rows

I am working on pandas manipulation and want to select only the last two rows for each column "B".

How to do without reset_index and filter (do inside groupby)

import pandas as pd
df = pd.DataFrame({
    'A': list('aaabbbbcccc'),
    'B': [0,1,2,5,7,2,1,4,1,0,2],
    'V': range(10,120,10)
})

df

My attempt

df.groupby(['A','B'])['V'].sum()

Required output

A  B
a  
   1     20
   2     30
b  
   5     40
   7     50
c  
   2    110
   4     80

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try:

df.sort_values(['A', 'B']).groupby(['A']).tail(2)

Output:

    A  B    V
1   a  1   20
2   a  2   30
3   b  5   40
4   b  7   50
10  c  2  110
7   c  4   80
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading