I would like to convert a table that looks like this:
Blue Green Red
Thing 1 No Yes No
Thing 2 Yes No No
Thing 3 Yes No No
Into this style:
Color
Thing 1 Green
Thing 2 Blue
Thing 3 Blue
Is there a nice way to do this in python or pandas? And do these table styles have names?
Just use idxmax:
df.eq('Yes').idxmax(axis=1)
Output
Thing 1 Green
Thing 2 Blue
Thing 3 Blue
dtype: object
If each row has exactly one 'yes', you can do
df.eq('Yes') @ df.columns
Output:
Thing 1 Green
Thing 2 Blue
Thing 3 Blue
dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With