Filtering pandas dataframe NaN values

20 views Asked by At

I have a pandas dataframe, 510 rows x 373 columns. The dataframe name is 'data'. There are 'NaN' values in a few rows, in a few columns (i.e. not all values are 'NaN' neither in a row nor column - just a few 'cells'). I am trying to filer out 'NaN' values. I would like to be able to filter out either rows or columns or rows AND columns containing 'NaN' - depending on my specific need.

It is easy to get what I believed to be a bool filter:

data.isna()

enter image description here

However, when I try to use the above expression as mask/ filter, it doesn't work. When I do data[data.isna()], data[data.isna() == True], filtered_data = data[data.isna()] I get the same result - 510 x 373 dataframe, all filled with 'NaN'.

When I do data.loc[data.isna()] I get 'ValueError: Cannot index with multidimensional key' error.

How can I create a dataframe containing only rows (or columns, or rows AND columns) from 'data' dataframe, which contain 'NaN'?

How can I create an opposite dataframe - i.e. the one which contains only rows (columns, rows and columns) from 'data', which are NOT 'NaN'?

I went through a dozen of explanations of np.nan, pd.NA, <'NA'> etc., but none helps with this trivial matter.

0

There are 0 answers