简体   繁体   English

如何在具有另一列最大值的行中的一个 dataframe 列中找到值?

[英]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,其中包含汽车的pricemake (以及其他)列,我需要找到最贵的汽车品牌。 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:解释:

  • use 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()在忽略NaNprice列中查找最大值的索引(这是基于skipna参数默认为True的默认行为,如文档中所示)
  • use loc[] to access the dataframe value in column make at the above index.使用loc[]访问上述索引处make列中的 dataframe 值。

暂无
暂无

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

相关问题 如何在数据框列中找到数组中的最大值? - How do I find the maximum value in an array within a dataframe column? 如何在 Pandas 数据框中找到最大值的行和列的索引? - How do I find the indices of the row and the column for the maximum value in a Pandas dataframe? 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 如何在 Pandas 中的串联 dataframe 中找到列/行组合的最大值 - How to find the maximum value of a column/row combination in a concatenated dataframe in Pandas 我如何找到:每列中的第一个非NaN值是DataFrame中该列的最大值吗? - How do I find: Is the first non-NaN value in each column the maximum for that column in a DataFrame? 根据另一列中的一列查找行值并进行计算 - Find row value based on one column in another column and do calculation 给定另一个 dataframe 中两列的值约束,在一个 dataframe 的列中查找最大值 - Find maximum of value in a column of one dataframe given the value constraint of two columns in another dataframe 如何在python数据帧中查找列的最大值 - How to find maximum value of a column in python dataframe 如何获取熊猫数据框列的最大值并在另一列中找到相应的值? - How do I take max value of a pandas dataframe column and find the corresponding value in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM