-2

I have a Pandas DataFrame with several categorical variables (1 or 0) and I would like to rename the 1 value as per its own variable name, how to do it?

          Name           Achiever  Arranger  Belief  Consistency  Deliberative  
0          Alex           1.0       0.0     0.0          0.0           0.0   
13         Luke           0.0       0.0     0.0          1.0           0.0     

I would like to return

          Name           Achiever  Arranger  Belief  Consistency  Deliberative  
0          Alex        Achiever      0.0     0.0          0.0           0.0   
13         Luke           0.0       0.0     0.0    Conistency          0.0 
Filippo Sebastio
  • 1,112
  • 1
  • 12
  • 23
  • Are you trying to rename the columns? have you looked at this post? [link](https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas/46912050#46912050) – Peter Jun 07 '18 at 02:32
  • 1
    Consider coming up with [MCVE] to increase the chances of getting answer. – harvpan Jun 07 '18 at 03:06

1 Answers1

0

You can try this:

for col in df.columns.values:
    df[col].replace(to_replace=1, value=col, inplace=True)
Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41