Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas dataframe query not working with where

I am new to pandas, I have this data frame:

df['educ1']

which gives

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1        4
2        3
3        3
4        4
5        1
        ..
28461    3
28462    2
28463    3
28464    2
28465    4
Name: educ1, Length: 28465, dtype: int64

when I try querying with

dt=df[df.educ1 > 1]

It’s working fine returning multiple rows, but when I try

college_grad_mask=(df.educ1 > 1)
df.where(college_grad_mask).dropna().head()

It gives 0 rows, I wonder what is wrong here?

>Solution :

You likely have NaNs in many columns, try to subset:

df.where(college_grad_mask).dropna(subset=['educ1']).head()

Or better:

df[college_grad_mask].head()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading