I have a dataframe, something like this
JavaScript
x
13
13
1
import pandas as pd
2
3
dic = {'animal':["cat", "dog", "rabbit"],
4
'colour': ["yellow", None, "red"],
5
'size':[None, "large", "small"]}
6
7
df = pd.DataFrame(dic)
8
9
animal colour size
10
0 cat yellow None
11
1 dog None large
12
2 rabbit red small
13
I want to create a new column that contains True
if any other column is null and False
if not.
JavaScript
1
5
1
animal colour size bool
2
0 cat yellow None True
3
1 dog None large True
4
2 rabbit red small False
5
How do I do this?
Advertisement
Answer
There are 2 functions for that: pandas.isna
and pandas.any
:
JavaScript
1
2
1
df['bool'] = df.isna().any(axis = 1)
2