如果数据结构与您描述的完全一致,则可以使用groupby / max:
>>> df
candidate_id enddate TitleLevel
0 1 2013.5.1 2
1 1 2013.5.1 4
2 1 2010.1.1 3
3 2 2012.12.1 2
>>> df.groupby(['candidate_id','enddate']).max().reset_index()
candidate_id enddate TitleLevel
0 1 2010.1.1 3
1 1 2013.5.1 4
2 2 2012.12.1 2
这里groupby对具有相等candidate_id和enddate的行进行分组,max()计算每个组中的最大TitleLevel.结果与删除所有其他值的行相同.
如果您有更多列,
>>> df
candidate_id enddate TitleLevel other_column
0 1 2013.5.1 2 foo
1 1 2013.5.1 4 bar
2 1 2010.1.1 3 foobar
3 2 2012.12.1 2 barfoo
您可以获取具有最大值的行的idex,而不必排序是否必须保留行顺序:
>>> idx = df.groupby(['candidate_id','enddate'],sort=False)['TitleLevel'].agg(lambda x: x.idxmax())
并使用ix过滤所需的行:
>>> df.ix[idx]
candidate_id enddate TitleLevel other_column
1 1 2013.5.1 4 bar
2 1 2010.1.1 3 foobar
3 2 2012.12.1 2 barfoo