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

Groupby pandas dataframe to produce a single list column of distinct groups and counts

still trying to think through how to describe this properly (will update question), but here’s my have/want minimal, reproducable, example of what I’m trying to do.

have = pd.DataFrame({'id': [1,1,1,2,2], 'grp': ['a', 'b', 'c', 'd', 'e'], 'val': [5,4,3,2,1]})
>>> have
   id grp  val
0   1   a    5
1   1   b    4
2   1   c    3
3   2   d    2
4   2   e    1

want = pd.DataFrame({'id': [1,2], 'results': [[('a', 5), ('b', '4'), ('c', 3)], [('d',2), ('e',1)]]})

>>> want
   id                   results
0   1  [(a, 5), (b, 4), (c, 3)]
1   2          [(d, 2), (e, 1)]

>Solution :

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

You can try groupby id column then zip the grp and val columns

out = (have.groupby('id')
       .apply(lambda g: list(zip(g['grp'], g['val'])))
       .rename('result')
       .reset_index())
print(out)

   id                    result
0   1  [(a, 5), (b, 4), (c, 3)]
1   2          [(d, 2), (e, 1)]
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