且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python pandas:按A列删除重复项,保留B列中具有最高值的行

更新时间:2023-01-20 18:41:04

这需要最后一个.虽然不是最大值:

This takes the last. Not the maximum though:

In [10]: df.drop_duplicates(subset='A', keep="last")
Out[10]: 
   A   B
1  1  20
3  2  40
4  3  10

您还可以执行以下操作:

You can do also something like:

In [12]: df.groupby('A', group_keys=False).apply(lambda x: x.loc[x.B.idxmax()])
Out[12]: 
   A   B
A       
1  1  20
2  2  40
3  3  10