Skip to content
Advertisement

Pandas convert dummies to a new column

I have a dataframe that discretize the customers into different Q’s, which looks like:

JavaScript

What I want to do is adding a new column, Q, to the dataframe which shows which sector this customer is in, so it looks like:

JavaScript

The only way I can think about is using for loop but it will give me a mess. Any other way to do this?

Advertisement

Answer

One option is to dump down into numpy:

Filter for just the Q columns:

JavaScript

Get the column positions that are equal to 1:

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