Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas groupby multiple columns exclusively

I have the DataFrame below and want to find the count of y and n for each column:

ID var1 var2
1 y
2 n y
3 y n
4 y n
5 y

the result would be like this:

var1_N var2_N
y 3 2
n 1 2

I used transform function but was wondering there is a better way to get the results.
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can just do value_counts on all columns you need to count using apply method, the results will be automatically joined on the index (or var value in your case):

df.filter(like='var').apply(lambda s: s.value_counts())

   var1  var2
y     3     2
n     1     2

Or use pd.value_counts directly:

df.filter(like='var').apply(pd.value_counts)

   var1  var2
y     3     2
n     1     2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading