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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With