数据框为一列选择最大值,但输出另一列的值
问题描述:
我有一个数据框,其值类似于下面
I have a dataframe with values similar to below
A10d B10d C10d A B C Strategy
20 10 5 3 5 1 3
该策略选择A10d,B10d,C10d的最大值并返回A,B,C的值 在这种情况下,A10d最大,策略返回A,值为3
The Strategy selects the max of A10d, B10d, C10d and return the value of A,B,C In this case A10d is the largest and Strategy returns A, value of 3
我不确定如何正确创建此策略"列,有人可以建议吗?非常感谢您的帮助
I am not sure how to create this Strategy column properly, can anyone advise please? Thank you very much for your help
答
I think you need iloc
for select first columns by positions and then get columns names by max
with idxmax
and replace
10d
by whitespace for match columns. Last create new column by lookup
:
print (df)
A10d B10d C10d A B C
0 20 10 5 3 5 1
1 20 100 5 3 5 1
df1 = df.iloc[:,:3]
print (df1)
A10d B10d C10d
0 20 10 5
1 20 100 5
s = df1.idxmax(axis=1).str.replace('10d','')
print (s)
0 A
1 B
dtype: object
df['Strategy'] = df.lookup(df.index, s)
print (df)
A10d B10d C10d A B C Strategy
0 20 10 5 3 5 1 3
1 20 100 5 3 5 1 5