Pandas - check if ALL values are NaN in Series

PythonPandasNullNanSeries

Python Problem Overview


I have a data series which looks like this:

print mys

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

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

My attempt:

pd.isnull(mys).all()

Output:

True

Is this the correct way to do it?

Python Solutions


Solution 1 - Python

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

mys.isnull().all()

Solution 2 - Python

This will check for all columns..

mys.isnull().values.all(axis=0)

Solution 3 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoosted_d16View Question on Stackoverflow
Solution 1 - PythonAndrew RosenfeldView Answer on Stackoverflow
Solution 2 - PythonReevesView Answer on Stackoverflow
Solution 3 - PythonEdwardView Answer on Stackoverflow