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

How to make sure, when you groupby lists. You dont get the grouped by rows, with empty lists

My issue is the following

I have the following sample data.

UserId OriginLink
A potato
B
A apple
B

When i run the following line of code.

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

prod.groupby(['UserId'])['OriginLink'].apply(lambda d : list(d)).reset_index()

I get the following result:

UserId OriginLink
A [potato,apple]
B [,,]

How can i guarantee that it doesn return the empty list in the B row. I tried a few if statement, but it didnt work

Wanted result

UserId OriginLink
A [potato,apple]

>Solution :

You can use a dropna if you want to drop anything that is null

prod.dropna(inplace = True)
prod.groupby(['UserId'])['OriginLink'].apply(lambda d : list(d)).reset_index()
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