0

Here's my dataset

Id    Column_A    Column_B    Column_C
1         Null           7        Null
2            8           7        Null
3         Null           8           7
4            8        Null           8

If at least one column is null, Combination will be Null

Id    Column_A    Column_B    Column_C   Combination
1         Null           7        Null         Null
2            8           7        Null         Null
3         Null           8           7         Null
4            8        Null           8         Null

2 Answers 2

3

Assuming Null is NaN, we could use isna + any:

df['Combination'] = df.isna().any(axis=1).map({True: 'Null', False: 'Notnull'})

If Null is a string, we could use eq + any:

df['Combination'] = df.eq('Null').any(axis=1).map({True: 'Null', False: 'Notnull'})

Output:

   Id Column_A Column_B Column_C Combination
0   1     Null        7     Null        Null
1   2        8        7     Null        Null
2   3     Null        8        7        Null
3   4        8     Null        8        Null
Sign up to request clarification or add additional context in comments.

Comments

1

Use DataFrame.isna with DataFrame.any and pass to numpy.where:

df['Combination'] = np.where(df.isna().any(axis=1), 'Null','Notnull')
df['Combination'] = np.where(df.eq('Null').any(axis=1), 'Null','Notnull')

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.