4

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?

jpf5046
  • 729
  • 7
  • 32
The Nightman
  • 5,609
  • 13
  • 41
  • 74
  • 1
    The names for these approaches are "dummy" or "indicator" variables in the first case, and a categorical variable in the second. See [`pandas.DataFrame.get_dummies()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.get_dummies.html), for example, which performs the conversion in the opposite direction (categorical to dummies). – NicholasM Dec 04 '19 at 20:43
  • only need to add df.eq(‘yes') – BENY Dec 04 '19 at 20:59

3 Answers3

6

Just use idxmax:

df.eq('Yes').idxmax(axis=1)

Output

Thing 1    Green
Thing 2     Blue
Thing 3     Blue
dtype: object
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
5

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
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
2

Use:

df.where(df.eq('Yes')).stack().reset_index(level=1)['level_1']

Thing 1    Green
Thing 2     Blue
Thing 3     Blue
Name: level_1, dtype: object
ansev
  • 30,322
  • 5
  • 17
  • 31