[英]How do I find the value in one dataframe column in the row with the maximum value of another column?
Given a dataframe with columns price
and make
(among others) for cars, I need to find the make of the car that is the most expensive.给定一个 dataframe,其中包含汽车的
price
和make
(以及其他)列,我需要找到最贵的汽车品牌。 I found the highest price using max(df['price']) but now I don't know how to use that price to find the make that correlates to it.我使用 max(df['price']) 找到了最高价格,但现在我不知道如何使用该价格找到与其相关的品牌。 Sample data can be found at this csv link:
示例数据可以在这个 csv 链接中找到:
https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv https://raw.githubusercontent.com/plotly/datasets/master/imports-85.csv
Here's a way to find the value in the make
column from the row with the max value in column price
:这是一种从
price
列中具有最大值的行中查找make
列中的值的方法:
make = df.loc[df.price.idxmax(),'make']
Output: Output:
mercedes-benz
Explanation:解释:
idxmax()
to find the index of the maximum value in the price
column ignoring NaN
(this is default behavior based on skipna
argument defaulting to True
as indicated in the docs)idxmax()
在忽略NaN
的price
列中查找最大值的索引(这是基于skipna
参数默认为True
的默认行为,如文档中所示)loc[]
to access the dataframe value in column make
at the above index.loc[]
访问上述索引处make
列中的 dataframe 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.