为不透明的问题名称道歉(不知道如何用词)。我有以下数据帧:
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
我想添加另一个返回文本的列,根据A,B,C
的哪一列是最高的:
例如,我想要Apple
如果A
是最高的,Banana
如果B
是最高的,Carrot
如果C
是最高的。因此,在上述示例中,4列的值应为:
New Col
Carrot
Apple
Banana
Carrot
任何帮助都将不胜感激!谢谢
@ShubhamSharma的答案比这更好,但这里有另一个选择:
输出:
使用^{} 沿} 连用:
axis=1
与^{结果:
相关问题 更多 >
编程相关推荐