Skip to content
Advertisement

Reverse a get_dummies encoding in pandas

Column names are: ID,1,2,3,4,5,6,7,8,9.

The col values are either 0 or 1

My dataframe looks like this:

JavaScript

I want the column names in front of the ID where the value in a row is 1.

The Dataframe i want should look like this:

JavaScript

Please help me in this, Thanks in advance

Advertisement

Answer

set_index + stack, stack will dropna by default

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement