Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading