Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pandas, how do I drop the last row of each group?

Tags:

python

pandas

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 rows of each group by doing:

print(grouped.agg(lambda x: x.iloc[-1]))

      B
A       
one    5
three  4
two    2

How can I drop the last row of each group instead? The result would be:

       A  B
0    one  0
1    one  1
3  three  3

I have tried filtering but it does not seem to do anything:

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 you

like image 452
user3465658 Avatar asked Mar 26 '14 19:03

user3465658


People also ask

How do I drop the last 10 rows in a Dataframe?

We can remove the last n rows using the drop() method. drop() method gets an inplace argument which takes a boolean value. If inplace attribute is set to True then the dataframe gets updated with the new value of dataframe (dataframe with last n rows removed).

How do I delete rows in pandas Dataframe based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


1 Answers

How 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]
like image 85
DSM Avatar answered Oct 05 '22 17:10

DSM