0

My dataframe is the following :

Files       attr_1     attr_2     attr_3 
file_1           X          X          X
file_2                      X
file_3           X                     X

I want to transform it to :

Files         attr   
file_1      attr_1       
file_1      attr_2    
file_1      attr_3     
file_2      attr_2                
file_3      attr_1      
file_3      attr_3         

The closest I found was this question : Reconstruct a categorical variable from dummies in pandas

However for me to use the stack() method I need to already have one line per file/attribute, ie the following :

Files       attr_1     attr_2     attr_3 
file_1           X         
file_1                      X          
file_1                                 X
file_2                      X
file_3           X                     
file_3                                 X

which is not my case unfortunately.

What is the cleanest way to achieve this ?

Vincent
  • 1,534
  • 3
  • 20
  • 42

1 Answers1

1

Firstly set Files as index and then stack(), followed by reset_index()

df = (df.set_index('Files')
      .stack()
      .reset_index()[['Files', 'level_1']]
      .rename(columns={'level_1': 'attr'})
      )
gyoza
  • 2,112
  • 2
  • 12
  • 18