Skip to content
Advertisement

Unstack and return value counts for each variable?

I have a data frame that records responses of 19717 people’s choice of programing languages through multiple choice questions. The first column is of course the gender of the respondent while the rest are the choices they picked. The data frame is shown below, with each response being recorded as the same name as column. If no response is selected, then this results in a NaN.

JavaScript

What I want is a table that returns the count based on Gender. Hence if 5000 men coded in Python and 3000 women in JS, then I should get this:

JavaScript

I have tried some of the options:

JavaScript

And it’s not what is required as described above. Can this be done in pandas?

Advertisement

Answer

Another idea would be to apply join values along axis 1, get_dummies then groupby:

JavaScript

[out]

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