简体   繁体   English

如何将多列中的最大值返回到 pandas df 中的新列

[英]How to return the highest value from multiple columns to a new column in a pandas df

Apologies for the opaque question name (not sure how to word it).为不透明的问题名称道歉(不知道如何措辞)。 I have the following dataframe:我有以下 dataframe:

import pandas as pd
import numpy as np

data = [['tom', 1,1,6,4],
        ['tom', 1,2,2,3],
        ['tom', 1,2,3,1],
        ['tom', 2,3,2,7],
        ['jim', 1,4,3,6],
        ['jim', 2,6,5,3]]

df = pd.DataFrame(data, columns = ['Name', 'Day','A','B','C']) 
df = df.groupby(by=['Name','Day']).agg('sum').reset_index()
df

在此处输入图像描述

I would like to add another column that returns text according to which column of A,B,C is the highest:我想添加另一列,根据A,B,C的哪一列最高返回文本:

For example I would like Apple if A is highest, Banana if B is highest, and Carrot if C is highest.例如,如果A最高,我想要Apple ,如果B最高,我想要Banana ,如果C最高,我想要Carrot So in the example above the values for the 4 columns should be:因此,在上面的示例中,4 列的值应该是:

New Col
Carrot
Apple
Banana
Carrot

Any help would be much appreciated!任何帮助将非常感激! Thanks谢谢

Use DataFrame.idxmax along axis=1 with Series.map :使用DataFrame.idxmax沿axis=1Series.map

dct = {'A': 'Apple', 'B': 'Banana', 'C': 'Carrot'}
df['New col'] = df[['A', 'B', 'C']].idxmax(axis=1).map(dct)

Result:结果:

  Name  Day  A   B  C New col
0  jim    1  4   3  6  Carrot
1  jim    2  6   5  3   Apple
2  tom    1  5  11  8  Banana
3  tom    2  3   2  7  Carrot

@ShubhamSharma's answer is better than this, but here is another option: @ShubhamSharma 的答案比这更好,但这是另一种选择:

df['New col'] = np.where((df['A'] > df['B']) & (df['A'] > df['C']), 'Apple', 'Carrot')
df['New col'] = np.where((df['B'] > df['A']) & (df['B'] > df['C']), 'Banana', df['New col'])

output: output:

    Name    Day A   B   C   New col
0   jim 1   4   3   6   Carrot
1   jim 2   6   5   3   Apple
2   tom 1   5   11  8   Banana
3   tom 2   3   2   7   Carrot

暂无
暂无

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

相关问题 如何使用 Pandas 从多列中确定最大值 - How to determine the highest value from multiple columns using Pandas Pandas df:用另一列中的特定值填充新列中的值(具有多列的条件) - Pandas df: fill values in new column with specific values from another column (condition with multiple columns) Map 多列来自 pandas dataframe 到字典并有条件地将值返回到新列 - Map multiple columns from pandas dataframe to a dictionary and conditionally return a value to a new column 如果等于另一列中的值,则熊猫从多列返回值 - Pandas return value from multiple columns if equal to value in another column 如何从pandas df中选择多个列并将其存储在另一个df中? - how to pick multiple columns from pandas df and store it in another df? 从 Pandas df 列名和值创建新列 - Creating new columns from pandas df column names and values 如何遍历 Pandas DF 中的列以检查某个值并返回同一行但来自不同列的值? - How to iterate over a column in a Pandas DF to check for a certain value and return a value in the same row but from a different column? 将值返回到pandas df中的新列 - Return values to new columns in a pandas df 如何将一些单元格值从 Pandas DF 中的 2 列移动到另一个新列? - How do I move some cell values from 2 columns in Pandas DF to another new column? Pandas 如何从一列创建重复列表,并且只保留对应列的最大值? - Pandas How do I create a list of duplicates from one column, and only keep the highest value for the corresponding columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM