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

Counting pairs of rows in pandas

I have this kind of Data Frame:

id  type
1   a
1   b
2   b
2   a
3   c
3   b

(Each ID has only 2 rows for sure)

I’d like to count the number of each pair, when a pair is the two types per ID.
I mean, to get that result for the previous table:

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

pair  count
(a, b)   2
(b, c)   1

Thanks!

>Solution :

You can use frozenset to have hashable, unordered objects to pass to value_counts:

df.groupby('id')['type'].agg(frozenset).value_counts()

output:

(a, b)    2
(b, c)    1
Name: type, dtype: int64

Note that the objects in the index are frozenset. I recommend to keep it this way (and to learn how to use them), but if you really need tuples:

out = df.groupby('id')['type'].agg(frozenset).value_counts()
out.index = out.index.map(tuple)
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