I have a dataframe with about 5 columns. The value I am looking to match could be present in either of the last 3 columns.
Key | col1 | col2 | col3 | col4
----------------------------------------
1 abc 21 22 23
2 cde 22 21 20
3 fgh 20 22 23
4 lmn 20 22 21
I am filtering on value 21 on any of the last three columns as follows:
df1 = df[(df['col2']=='21') | (df['col3']=='21') | (df['col4']=='21')]
which gives me
Key | col1 | col2 | col3 | col4
----------------------------------------
1 abc 21 22 23
2 cde 22 21 20
4 lmn 20 22 21
Using this new df1 I want to get this
Key | col1 | newCol
-------------------------
1 abc 21
2 cde 21
4 lmn 21
Basically any of the matched column as the new column value. How do I do this using pandas? I appreciate the help. So I was thinking may be I should filter and map it to the new column at the same time but I don't know how?