pandas check if column is null with query function

℡╲_俬逩灬. 提交于 2020-01-01 02:30:10

问题


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:


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


来源:https://stackoverflow.com/questions/46021195/pandas-check-if-column-is-null-with-query-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!