-1

I have a dataframe that can potentially contain NaN values. How do I find the NaNs and only the NaNs? Most suggestions suggest using df.isnull, but this returns None values as well. I only care about NaN.

Thanks

K Walsh
  • 39
  • 2
  • 7
  • Somewhat-broadly, you may find a big advantages removing the native Python None from your array if you can (perhaps you are)! Perhaps filtering them as a new boolean column. – ti7 May 17 '21 at 20:53

1 Answers1

1

To find NAN values you can use:

df.isna().sum(axis='index') # or axis = 0
df.isna().sum(axis='columns') # or axis = 1 
df.isna().sum().sum() # to find all nan values in df

NOTE: df.isna() will return a df(shape same as original df) with boolean values. Then you can use above 3 functions to evaluate nan values.

TO COUNT ONLY NAN's:

You can replace None with random_str, and then evaluate the NaN Count.

new_df = df.astype(str).replace('None','Random_str')
Nk03
  • 14,699
  • 2
  • 8
  • 22
  • 1
    So [df.isna()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isna.html#pandas-dataframe-isna) "Return a boolean same-sized object indicating if the values are NA. **NA values, such as None or numpy.NaN, gets mapped to True values.** Everything else gets mapped to False values." Additionally `isnull` is an "**Alias of isna.**" so this is an identical solution to the thing that is not working in OP's question. – Henry Ecker May 17 '21 at 21:01