Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I drop NA within pandas.DataFrame.query

Tags:

python

pandas

consider the df

df = pd.DataFrame(dict(A=[-1, 0, 1, np.nan, 2, np.nan, 3, 4]))

I can drop NA like this

df.dropna()

enter image description here

but how do I do it within the query method

df.query('A is not null')

doesn't work... what does?

like image 667
piRSquared Avatar asked Oct 28 '25 04:10

piRSquared


1 Answers

Idea #2: what's distinctive about NaNs is that they're not equal to themselves:

In [22]: df.query("A == A")
Out[22]: 
     A
0 -1.0
1  0.0
2  1.0
4  2.0
6  3.0
7  4.0

(The original idea #1 was to use df.query("A.notnull()"), but that only worked because numexpr wasn't installed in that environment, which limits the usefulness of query in the first place.)

like image 162
DSM Avatar answered Oct 30 '25 17:10

DSM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!