Pandas - check if ALL values are NaN in Series
Asked Answered
B

3

94

I have a data series which looks like this:

print mydf

id_L1
2       NaN
3       NaN
4       NaN
5       NaN
6       NaN
7       NaN
8       NaN

I would like to check if all the values are NaN.

My attempt:

pd.isnull(mydf).all()

Output:

True

Is this the correct way to do it?

Bonze answered 15/10, 2015 at 11:19 Comment(1)
yes, isnull will create a boolean series, all returns True if all are TrueIleneileo
O
154

Yes, that's correct, but I think a more idiomatic way would be:

mys.isnull().all()
Overwind answered 15/10, 2015 at 21:34 Comment(0)
L
23

This will check for all columns..

mys.isnull().values.all(axis=0)
Labor answered 11/7, 2019 at 12:37 Comment(2)
This is the right approach if you are searching "check if ALL values are NaN in DataFrame", like me. OP was searching for the Series solution Tho :P EDIT I prefer the version giving as result a Series: opp.isna().all()Edgardo
I would add '[0]' at the end to get to the actual "False" or True": mys.isnull().values.all(axis=0)[0]Kikelia
M
1
if df['col'].count() > 0:
    then ...

This works well but I think it might be quite a slow approach. I made the mistake of embedding this into a 6000-times loop to test four columns - and it's brutal, but I can blame the programmer clearly :)

Obviously, don't be like me. Always: Test your columns for all-null once, set a variable with the yes - "empty" or no - "not empty" result - and then loop.

Malarkey answered 30/4, 2021 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.