Skip to content
Advertisement

How to reverse a dummy variables from a pandas dataframe

I would like to reverse a dataframe with dummy variables. For example,

from df_input:

JavaScript

To df_output

JavaScript

I have been looking at the solution provided at Reconstruct a categorical variable from dummies in pandas but it did not work. Please, Any help would be much appreciated.

Many Thanks, Best Regards, Carlo

Advertisement

Answer

We can use wide_to_long, then select rows that are not equal to zero i.e

JavaScript

Update based on your edit :

JavaScript
Advertisement