lhmarsden
lhmarsden

Reputation: 186

Find if any column in pandas dataframe is NULL and state this in new column

I have a dataframe, something like this

import pandas as pd
 
dic = {'animal':["cat", "dog", "rabbit"],
        'colour': ["yellow", None, "red"],
        'size':[None, "large", "small"]}
 
df = pd.DataFrame(dic)

   animal  colour   size
0     cat  yellow   None
1     dog    None  large
2  rabbit     red  small

I want to create a new column that contains True if any other column is null and False if not.

   animal  colour   size   bool
0     cat  yellow   None   True
1     dog    None  large   True
2  rabbit     red  small  False

How do I do this?

Upvotes: 1

Views: 239

Answers (2)

Alessandro Gianfelici
Alessandro Gianfelici

Reputation: 44

You can use the following command:

df['bool'] = df.isna().sum(axis=1).astype(bool)

the idea behind this line of code is that True is casted to 1 in bool -> int conversions (implicitly done by the .sum(axis=1) method) and that any non zero integer is casted to True by int -> bool conversions (explicitly done by the .astype(bool) method).

Upvotes: 1

r.user.05apr
r.user.05apr

Reputation: 5456

There are 2 functions for that: pandas.isna and pandas.any:

df['bool'] = df.isna().any(axis = 1)

Upvotes: 3

Related Questions