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
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).
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With