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)
JavaScript
x
9
1
import pandas as pd
2
df = pd.DataFrame({
3
'A': list('aaabbbbcccc'),
4
'B': [0,1,2,5,7,2,1,4,1,0,2],
5
'V': range(10,120,10)
6
})
7
8
df
9
My attempt
JavaScript
1
2
1
df.groupby(['A','B'])['V'].sum()
2
Required output
JavaScript
1
11
11
1
A B
2
a
3
1 20
4
2 30
5
b
6
5 40
7
7 50
8
c
9
2 110
10
4 80
11
Advertisement
Answer
Try:
JavaScript
1
2
1
df.sort_values(['A', 'B']).groupby(['A']).tail(2)
2
Output:
JavaScript
1
8
1
A B V
2
1 a 1 20
3
2 a 2 30
4
3 b 5 40
5
4 b 7 50
6
10 c 2 110
7
7 c 4 80
8