Liam Hanninen
Liam Hanninen

Reputation: 1573

How to filter DataFrame using isna?

This seems super basic and yet I am failing to filter this dataframe. As you can see from the screenshot I load a very basic set of data. I check if any values in column 'Col3' is na. And finally I try to filter the dataframe using that. I am hoping to get returned just the second column (with index 1). But as you can see I get all 5 rows but the values for Col3 are now all NaN.

I am using Python 3.7.3 and Pandas 1.1.4

enter image description here

Trying wwnde's suggestion to use brackets instead of .loc did not seem to work: enter image description here

Upvotes: 1

Views: 2500

Answers (1)

wwnde
wwnde

Reputation: 26676

Try

data( now that you didnt give me sample data)

df = pd.DataFrame({'group1': [0, 0, 1, 1], 'group2': [2, 2, 3, 4],
                   'base': [0, 1, 2, 3], 'x1': [3, 4, 5, 6], 'x2': [np.nan, 6, np.nan, 8]})



df[df['x2'].isna()]

      group1  group2  base  x1  x2
0       0       2     0     3  NaN
2       1       3     2     5  NaN

Use loc accessor if you need to call particular columns

df.loc[df['x2'].isna(),:'base']#base and preceding columns



    group1  group2  base
0       0       2     0
2       1       3     2

or

df.loc[df['x2'].isna(),['base','x1']]

    base  x1
0     0   3
2     2   5

Upvotes: 1

Related Questions