Home > database >  Null/duplicate check in a column based on another column filter
Null/duplicate check in a column based on another column filter

Time:01-03

I am working on pandas with the below requierment

enter image description here

I need to check the below conditions if criteria is A, then m shouldn't be null if criteria is B then n shouldn't be null

I wrote the below code for it

df_filter = df.loc[df['criteria']]=='A',[m]] #for A condition check

or

df_filter = df.query("criteria == A")[m]

but both are not giving correct result

I have also tried

df_filter = df.loc[(df["criteria"] == "A") & ~ (df["m"].isnull()]

but this giving the columns without null..

I need to check if there are any null values exist in m column if A is selected from criteria.

Any help would be appreciated

CodePudding user response:

Use Series.notna for test not missing values, if need chain another condition use | for bitwise OR:

df_filter = df[(df["criteria"].eq("A") & df["m"].notna()) | 
                (df["criteria"].eq("B") & df["n"].notna())]

If no values are empty strings:

df_filter = df[(df["criteria"].eq("A") & df["m"].ne('')) | 
                (df["criteria"].eq("B") & df["n"].ne(''))]
  • Related