I have a dataframe:
import pandas as pd data = {'Alpha': [1, 2, 3, 4, 5], 'Beta':[6, 7 ,8, 9, 10], 'Delta': [11, 12, 13, 14, 15], 'Gamma': [16, 17, 18, 19, 20]} df = pd.DataFrame(data, index = ['2010', '2011', '2012', '2013', '2014']) df
It also has a column 'Select', holding other column names
df['Select'] = ['Beta', 'Gamma', 'Gamma', 'Delta', 'Alpha'] df
I need to add a column, say 'Choice', where data will be derived from the columns, names of which are held in 'Select' column, like below:
df['Choice'] = [0,0,0,0,0] row = df['Select'].values for i in range(len(row)): df['Choice'][i] = df[row[i]][i] df
- I'm doing it using a loop, but maybe there is some more elegant way? Thank you!


