Reputation: 391
For a given Pandas DataFrame row, how can I use the value from one column to return the value of another column which name is the value from the first column. E.g.
if row['column_name'] == 'col_A':
return row['col_A']
if row['column_name'] == 'col_B':
return row['col_B']
The same way you would think of getting the max value when knowing the agrmax in numpy. E.g.
import pandas as pd
import numpy as np
df = pd.DataFrame({'col_A': [1,2,3], 'col_B': [2,3,0]})
df['col_C-max'] = df.apply(np.max, axis = 1)
df['col_D-colum_name_of_max_value'] = df[['col_A',
'col_B']].apply(np.argmax, axis = 1)
In this case getting col_C when knowing only the col_A, col_B, and col_D:
col_A col_B col_C-max col_D-colum_name_of_max_value
0 1 2 2 col_B
1 2 3 3 col_B
2 3 0 3 col_A
Upvotes: 1
Views: 1676
Reputation: 214927
You can use pandas 'fancy indexing' lookup
, which pairs the index and the column names and pick up one value for each pair; and in this case it will be for each index(row), it picks up the value under the corresponding column from col_D
:
df = pd.DataFrame({'col_A': [1,2,3], 'col_B': [2,3,0], 'col_D': ['col_B', 'col_B', 'col_A']})
df['col_C'] = df.lookup(df.index, df.col_D)
df
# col_A col_B col_D col_C
#0 1 2 col_B 2
#1 2 3 col_B 3
#2 3 0 col_A 3
Upvotes: 2