Kilo_Papa
Kilo_Papa

Reputation: 35

Fetch column value based on dynamic input

I have a dataframe, where in I have 1 column, which contains names of column satisfying certain conditions for each row.

It's like if columns of dataframe are Index, Col1, Col2, Col3, Col_Name. Where Col_Name has either Col1 or Col2 or Col3 for each row.

Now in a new column say Col_New, I want output for each row such as if 5th row Col_Name mentions Col_1, then value of Col_1 in 5th row.

I am sorry I cannot post the code I am working on, hence gave this hypothetical example.

Obliged for any help, thanks.

Upvotes: 1

Views: 108

Answers (3)

jezrael
jezrael

Reputation: 862841

In pandas is for this function DataFrame.lookup, also it seems need same types of values in columns and looking column, so is possible convert both to strings:

np.random.seed(123)
cols = [1, 2, 3]
df = pd.DataFrame(np.random.randint(10, size=(5, 3)), columns=cols).rename(columns=str)
df['Col_Name'] = np.random.choice(cols, 5)

df['Col_New'] = df.lookup(df.index, df['Col_Name'].astype(str))
print(df)

   1  2  3  Col_Name  Col_New
0  2  2  6         3        6
1  1  3  9         2        3
2  6  1  0         1        6
3  1  9  0         1        1
4  0  9  3         1        0

Upvotes: 0

EddyG
EddyG

Reputation: 685

Using the example DataFrame from Chris A.

You could do it like this:

cols = ['Col1', 'Col2', 'Col3']
df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df['Col_Name'] = np.random.choice(cols, 10)

print(df)
df['Col_New'] = [df.loc[df.index[i],j]for i,j in enumerate(df.Col_Name)]

print(df)

Upvotes: 0

Chris Adams
Chris Adams

Reputation: 18647

IIUC you could use:

df['col_new'] = df.reset_index().apply(lambda x: df.at[x['index'], x['col_name']], axis=1)

Example:

cols = ['Col1', 'Col2', 'Col3']

df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df['Col_Name'] = np.random.choice(cols, 10)
print(df)

       Col1      Col2      Col3 Col_Name
0  0.833988  0.939254  0.256450     Col2
1  0.675909  0.609494  0.641944     Col3
2  0.877474  0.971299  0.218273     Col3
3  0.201189  0.265742  0.800580     Col2
4  0.397945  0.135153  0.941313     Col2
5  0.666252  0.697983  0.164768     Col2
6  0.863377  0.839421  0.601316     Col2
7  0.138975  0.731359  0.379258     Col3
8  0.412148  0.541033  0.197861     Col2
9  0.980040  0.506752  0.823274     Col3    

df['Col_New'] = df.reset_index().apply(lambda x: df.at[x['index'], x['Col_Name']], axis=1)

[out]

       Col1      Col2      Col3 Col_Name   Col_New
0  0.833988  0.939254  0.256450     Col2  0.939254
1  0.675909  0.609494  0.641944     Col3  0.641944
2  0.877474  0.971299  0.218273     Col3  0.218273
3  0.201189  0.265742  0.800580     Col2  0.265742
4  0.397945  0.135153  0.941313     Col2  0.135153
5  0.666252  0.697983  0.164768     Col2  0.697983
6  0.863377  0.839421  0.601316     Col2  0.839421
7  0.138975  0.731359  0.379258     Col3  0.379258
8  0.412148  0.541033  0.197861     Col2  0.541033
9  0.980040  0.506752  0.823274     Col3  0.823274

Example 2 (based on integer col references)

cols = [1, 2, 3]
np.random.seed(0)
df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df[13] = np.random.choice(cols, 10)

print(df)

        1         2         3   13
0  0.548814  0.715189  0.602763   3
1  0.544883  0.423655  0.645894   3
2  0.437587  0.891773  0.963663   1
3  0.383442  0.791725  0.528895   3
4  0.568045  0.925597  0.071036   1
5  0.087129  0.020218  0.832620   1
6  0.778157  0.870012  0.978618   1
7  0.799159  0.461479  0.780529   2
8  0.118274  0.639921  0.143353   2
9  0.944669  0.521848  0.414662   3

Instead use:

df['Col_New'] = df.reset_index().apply(lambda x: df.at[int(x['index']), int(x[13])], axis=1)

          1         2         3  13   Col_New
0  0.548814  0.715189  0.602763   3  0.602763
1  0.544883  0.423655  0.645894   3  0.645894
2  0.437587  0.891773  0.963663   1  0.437587
3  0.383442  0.791725  0.528895   3  0.528895
4  0.568045  0.925597  0.071036   1  0.568045
5  0.087129  0.020218  0.832620   1  0.087129
6  0.778157  0.870012  0.978618   1  0.778157
7  0.799159  0.461479  0.780529   2  0.461479
8  0.118274  0.639921  0.143353   2  0.639921
9  0.944669  0.521848  0.414662   3  0.414662

Upvotes: 1

Related Questions