Python Pandas print column name if it match reference column

Question:

I’m trying to print the column name in a new column ‘result’ if the ‘b’ or ‘c’ match (equal) ‘a’ for every row.

This is an example, I have more than 10 column names.

my df is like this:

   a   b   c
a  0   1   2
b  4   4   5
c  6   7   6
d  9   9  11

expected result:

   a   b   c   result
a  0   1   2      NaN 
b  4   4   5        b
c  6   7   6        c
d  9   9  11        b
Asked By: One boy

||

Answers:

Use idxmax on the comparison of b/c and a (with eq).

Also use a mask (with where) to ensure there is at least one valid match as idxmax would return the first False index if no True is found:

m = df[['b', 'c']].eq(df['a'], axis=0)

df['result'] = m.idxmax(1).where(m.any(1))

output:

   a  b   c result
a  0  1   2    NaN
b  4  4   5      b
c  6  7   6      c
d  9  9  11      b

intermediate m:

       b      c
a  False  False
b   True  False
c  False   True
d   True  False
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.