【发布时间】:2014-03-26 19:13:56
【问题描述】:
我有一个如下图所示的数据框:
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
我可以通过以下方式轻松选择每组的最后一行:
print(grouped.agg(lambda x: x.iloc[-1]))
B
A
one 5
three 4
two 2
如何改为删除每个组的最后一行?结果是:
A B
0 one 0
1 one 1
3 three 3
我尝试过过滤,但它似乎没有做任何事情:
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
谢谢
【问题讨论】: