[英]How do I take max value of a pandas dataframe column and find the corresponding value in another column?
I want to take max value of a pandas dataframe column and find the corresponding value in another column?我想获取熊猫数据框列的最大值并在另一列中找到相应的值? Let's assume there are not duplicates in the maximum value, so you are always returning only one value.
假设最大值中没有重复项,因此您始终只返回一个值。 For example,
例如,
GPA ID
2.3 Tina1
3.4 Bob1
3.6 Lia1
2.9 Tina2
4.0 Blake1
4.5 Conor2
Here the max GPA is 4.5, but I want to return the corresponding id for the max GPA, so I would return Conor2.这里的最大 GPA 是 4.5,但我想返回最大 GPA 对应的 id,所以我会返回 Conor2。 I am unsure of how to do this, so help would be greatly appreciated :) Thanks!
我不确定如何执行此操作,因此非常感谢您的帮助:) 谢谢!
You can use您可以使用
df.loc[df.GPA == df.GPA.max(), 'ID']
You get你得到
5 Conor2
If you don't want the index but just the value, try如果您不想要索引而只想要值,请尝试
df.loc[df.GPA == df.GPA.max(), 'ID'].values[0]
You get你得到
'Conor2'
Let's use idxmax
:让我们使用
idxmax
:
df.set_index('ID').idxmax()
Output:输出:
GPA Conor2
dtype: object
A series with a single value Conor2
.具有单个值
Conor2
系列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.