How to check for NaN in any data structure in python?

Viewed 127

Is it possible to check for NaN in any arbitrary data structure in python?

Essentially, I would like a function, def containsNan(d) which would return True for any arbitrary standard python data structure d, if there is any NaN inside d.

I looked into flattening d, before doing some kind of apply(), with math.isnan() but there doesn't appear to be a general flatten that works on any d.

Any ideas on what approach I should try?

1 Answers

This is very easy. Try using the pandas module for this job. It has a variety of functions for dealing with NaNs.

Check for null values:

df.isnull().values.any()

Count the number of null values:

df.isnull().sum().sum()

Note: This works in a pandas data frame. So if you want to work with a list or something convert it before doing this

Related