Home > Blockchain >  Check whether ALL missing values have the same value in another column in a pandas df
Check whether ALL missing values have the same value in another column in a pandas df

Time:04-05

I've got a pandas dataframe that looks like this:

x    y
0    ny
21   ch
NaN  ap
21   ca
NaN  ap

All missing values (NaN) in the column x should have the value ap in the column y. How can I check whether this is true? I'm looking for a line of code that performs this check automatically, and returns True or False.

EDIT: This question was closed and marked as duplicate here. But the suggested answer isn't at all relevant to what I'm asking here. Hence, I've asked the question again.

CodePudding user response:

If need test if missing values has only NaNs filter first all misng rows by x and then test if all values y are ap:

test = df.loc[df['x'].isna(), 'y'].eq('ap').all()

CodePudding user response:

you can try to filter your dataframe to check the remaining values. Assuming your dataframe name is df, you can try this.

verify_df = df.loc[(df['x'] == NaN) & (df['y'] == 'ap')]
verify_df

  • Related