Using Pandas, how do I delete the last line of each group?

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

+4
source share
2 answers

What about:

>>> df.groupby("A", as_index=False).apply(lambda x: x.iloc[:-1])
       A  B
0    one  0
1    one  1
3  three  3

[3 rows x 2 columns]
+7
source

Rather, you can use cumcount :

In [11]: df[grouped.cumcount(ascending=False) > 0]
Out[11]: 
       A  B
0    one  0
1    one  1
3  three  3
+2
source

Source: https://habr.com/ru/post/1533597/


All Articles