1

You have data in Pandas dataframe like

>> pd.DataFrame({'Cat' : [1,0,0], 'Fish' : [0,0,1], 'Parrot' : [0,1,0]})

Cat | Fish | Parrot
1   |  0   |   0
0   |  0   |   1
0   |  1   |   0

and you want

Animal
Cat
Parrot
Fish

how can you unexplode the boolean columns into the multivariate column with Animal heading?

hhh
  • 50,788
  • 62
  • 179
  • 282
  • If need something else, let me know, answer should be reopened. – jezrael Aug 14 '18 at 13:09
  • 1
    For me working nice `x = df.stack() df = pd.Series(pd.Categorical(x[x!=0].index.get_level_values(1))).to_frame('Animal')` – jezrael Aug 14 '18 at 13:10
  • @jezrael it is hard to follow the logic there: you create straight table, then I get lost. Can you elaborate the parts in an answer? – hhh Aug 14 '18 at 13:15
  • I try explain a bit. First need `stack` for `MultiIndex Series`, then filter it by `1` and get second levev of `MultiIndex`. Last convert it to `Series` – jezrael Aug 14 '18 at 13:18
  • 1
    I completely agree with the dup target. Also `df.dot(df.columns).rename('Animal')` – piRSquared Aug 14 '18 at 13:19
  • 1
    I've added my answers to the dup target. Hopefully you find the answers helpful. If you have questions, please comment on the answer there. https://stackoverflow.com/a/51842666/2336654 – piRSquared Aug 14 '18 at 13:33
  • @piRSquared df.dot is by far the easiest to understand, thank you. It would be useful here as well, the other thread hard reading. – hhh Aug 14 '18 at 13:35

0 Answers0