skip to Main Content

I have following scenerio where I have to check for values in a column and its corresponding values in other columns. If any one of the value is not null then need to return true.
enter image description here

Suppose I have to check for values D, E and F in column1 and check its corresponding values in column2 and column3. Here column2 contains NaN but column3 contains data then return true

2

Answers


  1. If I understand correctly, lets say you have the following data frame:

    import numpy as np
    import pandas as pd
    df=pd.DataFrame({"Column1":['A','B','C','D'],'Column2':[1,2,3,np.nan],'Column3':['x','y',np.nan,np.nan]})
    print(df)
    
      Column1  Column2 Column3
    0       A      1.0       x
    1       B      2.0       y
    2       C      3.0     NaN
    3       D      NaN     NaN
    

    Then you should first set the column you want to check(Column1 in our case) as an index and do the following:

    df.set_index('Column1').notna().any(axis=1)
    
    Column1
    A     True
    B     True
    C     True
    D    False
    
    Login or Signup to reply.
  2. Assuming you want to test if all rows of the target have at least one non-NaN value (any), use:

    target = ['D', 'E', 'F']
    
    out = (df.loc[df['Column1'].isin(target), ['Column2', 'Column3']]
             .notna().any(axis=1)
             .all()
          )
    

    output: True (but would be False if value1 was NaN)

    If you want to check if there is at least one non-NaN cell, use any in both cases:

    target = ['D', 'E', 'F']
    
    out = (df.loc[df['Column1'].isin(target), ['Column2', 'Column3']]
             .notna().any(axis=1)
             .any()
          )
    

    output: True (and would still be True if value1 was NaN)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search