1

I want to drop rows containing NaN values except if a separate column contains a specific string. Using the df below, I want to drop rows if NaN in Code2, Code3 unless the string A is in Code1.

df = pd.DataFrame({   
        'Code1' : ['A','A','B','B','C','C'],    
        'Code2' : ['B',np.nan,'A','B',np.nan,'B'],    
        'Code3' : ['C',np.nan,'C','C',np.nan,'A'],                                   
        })

def dropna(df, col):

    if col == np.nan:
        df = df.dropna()

    return df

df = dropna(df, df['Code2'])

Intended Output:

  Code1 Code2 Code3
0     A     B     C
1     A   NaN   NaN
2     B     A     C
3     B     B     C
4     C     B     A

1 Answer 1

3

Use DataFrame.notna + DataFrame.all to performance a boolean indexing:

new_df=df[df.Code1.eq('A')|df.notna().all(axis=1)]
print(new_df)

  Code1 Code2 Code3
0     A     B     C
1     A   NaN   NaN
2     B     A     C
3     B     B     C
5     C     B     A
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.