I have a column containing the names of certain columns of a dataframe.
import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4], 'col3':['col1', 'col2']}
df = pd.DataFrame(data=d)
Now I would like to create a col4 with values from either col1 (first row) or col2 (second row), based on the names in col3.
CodePudding user response:
Use indexing lookup:
idx, cols = pd.factorize(df['col3'])
df['col4'] = df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx]
Output:
col1 col2 col3 col4
0 1 3 col1 1
1 2 4 col2 4
CodePudding user response:
you can use df.eval
df['col4']=df.apply(lambda row:df.eval(df.col3)[row.name][row.name],axis=1)
df
Out[233]:
col1 col2 col3 col4
0 1 3 col1 1
1 2 4 col2 4