3

I have a dataframe with values similar to below

A10d  B10d  C10d  A   B   C   Strategy
20    10    5     3   5   1    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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jezrael this works well, you saved my day again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.