Pandas.dataframe.query () - select zero rows (Pandas is equivalent to SQL: "IS NOT NULL")

I am extracting rows with some values ​​from a pandas dataframe with the following code. I need to convert this code to pandas.query ().

results= rs_gp[rs_gp['Col1'].notnull()] 

When I convert to: results= rs_gp.query('Col1!=None')

He gives me the error "None is not defined". Can someone help.

Thank you Mercury

+5
source share
1 answer

We can use the fact that NaN != NaN :

 In [1]: np.nan == np.nan Out[1]: False 

Thus, comparing a column with itself will only return values ​​other than NaN:

 rs_gp.query('Col1 == Col1') 

Demo:

 In [42]: df = pd.DataFrame({'Col1':['aaa', np.nan, 'bbb', None, '', 'ccc']}) In [43]: df Out[43]: Col1 0 aaa 1 NaN 2 bbb 3 None 4 5 ccc In [44]: df.query('Col1 == Col1') Out[44]: Col1 0 aaa 2 bbb 4 5 ccc 
+6
source

All Articles