lrh09
lrh09

Reputation: 587

Pandas Use a column consists of column names to populate the values dynamically into another column

I would like to obtain the 'Value' column below, from the original df:

    A   B   C   Column_To_Use
0   2   3   4   A            
1   5   6   7   C            
2   8   0   9   B            


    A   B   C   Column_To_Use   Value       
0   2   3   4   A               2
1   5   6   7   C               7
2   8   0   9   B               0

Upvotes: 2

Views: 32

Answers (1)

jezrael
jezrael

Reputation: 862661

Use DataFrame.lookup:

df['Value'] = df.lookup(df.index, df['Column_To_Use'])
print (df)
   A  B  C Column_To_Use  Value
0  2  3  4             A      2
1  5  6  7             C      7
2  8  0  9             B      0

Upvotes: 4

Related Questions