1

Have this data in panda dataframe:

animal      tall    swim        jump  
frog          0        1            1
toad          1        0            0
tadpole       0        0            1

Would like this output:

frog swim
frog jump
toad tall
tadpole jump

Any builtin function for this?

tooptoop4
  • 234
  • 3
  • 15
  • 45

1 Answers1

1

Use set_index with stack, filter 1 and create DataFrame by contructor:

df1 = df.set_index('animal').stack()
s = df1[df1==1]

df2 = pd.DataFrame({'a':s.index.get_level_values(0),
                    'b':s.index.get_level_values(1)})
print (df2)
         a     b
0     frog  swim
1     frog  jump
2     toad  tall
3  tadpole  jump

Another solution is use MultiIndex.to_frame:

df2 = s.index.to_frame(index=False)
df2.columns = ['a','b']
print (df2)
         a     b
0     frog  swim
1     frog  jump
2     toad  tall
3  tadpole  jump

Different solution with melt and filtering by query:

df2 = (df.melt('animal', var_name='val')
         .query('value == 1')
         .sort_values('animal')
         .reset_index(drop=True)
         .drop('value', axis=1))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252