29

I have pandas dataframe that I want to execute on it query function with isnull() or not isnull() condition like that:

In [67]: df_data = pd.DataFrame({'a':[1,20,None,40,50]})
In [68]: df_data
Out[68]:       a
         0   1.0
         1  20.0
         2   NaN
         3  40.0
         4  50.0

if I use this command:

df_data.query('a isnull', engine='python')

or this command:

df_data.query('a isnull()', engine='python')

I get an error:

In [75]: df_data.query('a isnull', engine='python')  
File "<unknown>", line 1    a isnull           
SyntaxError: invalid syntax

In [76]: df_data.query('a isnull()', engine='python')  
File "<unknown>", line 1    a isnull ()           
SyntaxError: invalid syntax

What is the right way to do that?

Thank you.

1 Answer 1

60

Use .:

a = df_data.query('a.isnull()', engine='python')
print (a)
    a
2 NaN

b = df_data.query('a.notnull()', engine='python')
print (b)
      a
0   1.0
1  20.0
3  40.0
4  50.0

You can use also logic NaN != NaN:

a = df_data.query('a != a')
print (a)
    a
 2 NaN

b = df_data.query('a == a')
print (b)
      a
0   1.0
1  20.0
3  40.0
4  50.0
Sign up to request clarification or add additional context in comments.

4 Comments

df_data.query('a.isnull()') works without engine='python'
I can't get this to work without the engine='python'
I don't think df_data.query('a.isnull()') works in Pandas 1.x, not sure what broke it.
I really like the a == a trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.