Given a dataframe with columns a1, b1 and a2, b2, I want to find the column index for the largest value out of a1, b1 and then get the value with the same relative column index out of a2, b2, as shown in the want column below:
import pandas as pd
import numpy as np
# Sample data
df= pd.DataFrame({'a_1':[1,2,3], 'b_1': [2,1,3], 'a_2': [3,4,7], 'b_2':[5,6,8], 'want':[5, 4, 7]})
I was able to get this far, but I'm not sure what the best approach is for the final step:
# Get the argmax for a1, b1
df['c'] = df[['a_1', 'b_1']].idxmax(axis=1)
# Get the column index of the argmax
df['d'] = df['c'].apply(lambda x: ['a_1', 'b_1'].index(x))
This is a simplified version of the problem - there are actually many more columns to search through - e.g. a1-z1, a2-z2.