简体   繁体   English

如何获取熊猫数据框列的最大值并在另一列中找到相应的值?

[英]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.

相关问题 Pandas Dataframe:groupby id查找最大列值并返回另一列的对应值 - Pandas Dataframe: groupby id to find max column value and return corresponding value of another column Pandas:在列的每一行中查找最大值,并在另一列中标识相应的值 - Pandas: Find max value in each row of a column and identify corresponding values in another column 如何在 Pandas 数据框列中找到已知值的索引? - How do I find the index of a known value in a pandas dataframe column? 在整个数据框中查找最大值和相应的列/索引名称 - Find max value and the corresponding column/index name in entire dataframe Pandas dataframe,在一行中,查找所选列中的最大值,并根据该值查找另一列的值 - Pandas dataframe, in a row, to find the max in selected column, and find value of another column based on that 如何在特定列中找到最大值并在数据框的另一列中返回相应值? - How do I find the maximum value in specific columns and return the corresponding value in another column of my data frame? 将 dataframe 分组在一列上,并从一列中获取最大值,从另一列中获取相应的值 - Group a dataframe on one column and take max from one column and its corresponding value from the other col 如何根据 python 中另一列的条件查找两个日期之间特定列的最大值 - How do I Find max value of a particular column between 2 dates based on a condition from another column in python 对应于pandas DataFrame中最大值的列名 - Column name corresponding to largest value in pandas DataFrame 对于每个类别,如何找到另一列的最小值对应的列的值? - For each category, how to find the value of a column corresponding to the minimum of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM