Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas.dataframe.query() - fetch not null rows (Pandas equivalent to SQL: "IS NOT NULL")

I am fetching the 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')

It gives me the error

None is not defined
like image 451
Rtut Avatar asked Jun 16 '16 15:06

Rtut


1 Answers

We can use the fact that NaN != NaN:

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

So comparing column to itself will return us only non-NaN values:

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
like image 163
MaxU - stop WAR against UA Avatar answered Oct 22 '22 15:10

MaxU - stop WAR against UA