简体   繁体   English

使用python pandas获取每个组中具有最大值的行

[英]Get row which has max value in each group using python pandas

I have a dataframe.我有一个数据框。 It is group by 'ex' as below: enter image description here Now I want to get rows which have max value in each group它按“ex”分组,如下所示:在此处输入图像描述现在我想获取每个组中具有最大值的行

so a quick google search and you will find out how to get relevant rowshere : for your implementation:所以一个快速的谷歌搜索,你会发现如何在这里获得相关的行:对于你的实施:

df.groupby("ex")['transaction_fee'].max()

the other thing you should look for is how to get the corresponding indices for the original table.您应该寻找的另一件事是如何获取原始表的相应索引。 you could find something similar here :你可以在这里找到类似的东西:

for your implemtionation:为了您的实施:

idxs = df.groupby("ex")['transaction_fee'].transform(max) == df['transaction_fee']
relevant_df = df[idxs]

cheers ✌干杯✌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM