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

Time:05-24

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.

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]

CodePudding user response:

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()
  • Related