简体   繁体   English

根据条件选择列中的最大值

[英]Selecting maximum value in a column based on condition

I have two columns. 我有两列。 ID and Percentage. ID和百分比。 Some IDs are not unique. 有些ID不是唯一的。 Assume I have IDs 233, 233, 277, 277. And corresponding percentages: 4,5%, 7%, 3%, 1%. 假设我有ID 233、233、277、277。以及相应的百分比:4.5%,7%,3%,1%。 I need to select max. 我需要选择最高 percentage for each ID. 每个ID的百分比。 So that outcome is: 233 - 7%, 277 - 3%. 结果是:233-7%,277-3%。

I wrote code that returns max value for the whole column, not the specific non-unique ID. 我写的代码返回整个列的最大值,而不是特定的非唯一ID。

df['help_column'] = np.where(df.duplicated() ==True, max(df['percentage']),0)

As the highest value in the whole column is 33%, I get 33% for ID 233, and 33% for ID 277 instead of desired result. 因为整个列中的最高值为33%,所以ID 233为33%,ID 277为33%,而不是期望的结果。 Thanks 谢谢

这更像是一种transform

df['help_column'] = df.groupby('ID')['percentage'].transform('max')

尝试这个

df.groupby(['ID'])['percentage'].max()

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

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