I have a dataframe as shown below:
import pandas as pd
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three', 'three', 'one'], 'B': range(6)})
grouped = df.groupby('A')
print grouped.head()
A B
A
one 0 one 0
1 one 1
5 one 5
three 3 three 3
4 three 4
two 2 two 2
I can easily select the last lines of each group by doing:
print grouped.agg(lambda x: x.iloc[-1])
B
A
one 5
three 4
two 2
How can I remove the last row from each group? The result will be:
A B
0 one 0
1 one 1
3 three 3
I tried filtering but seems to do nothing:
print grouped.filter(lambda x: x.iloc[-1])
A B
0 one 0
1 one 1
5 one 5
3 three 3
4 three 4
2 two 2
thank
source
share