Python – If the group contains empty columns, the row is deleted

If the group contains empty columns, the row is deleted… here is a solution to the problem.

If the group contains empty columns, the row is deleted

I have multiple rows in my data frame and a column called Name.

If any row has empty columns (nan, none, or empty string), I want to group by “name” and remove each item in the group.

How can I do this?

Enter

       name    c0  c1  c2
193556   INFO    1       
273142    OMN    1  1   1
256278    OMN    1  1   1
41165    INFO    1  1   1
339649   INFO    1  1   1

Output

       name    c0  c1  c2
273142    OMN    1  1   1
256278    OMN    1  1   1

Solution

You can use filter, check for null values ("") in the group and filter based on that condition:

import pandas as pd

df.groupby('name').filter(lambda x: (x != "").all().all())

Related Problems and Solutions