英文:
pandas dataframe query not working with where
问题
我是新手使用pandas,我有这个数据框:
df['educ1']
它给出了:
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
当我尝试使用以下查询时:
dt = df[df.educ1 > 1]
它正常工作并返回多行,但是当我尝试以下方式时:
college_grad_mask = (df.educ1 > 1)
df.where(college_grad_mask).dropna().head()
它返回0行,我想知道这里有什么问题?
英文:
I am new to pandas, I have this data frame:
df['educ1']
which gives
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?
答案1
得分: 1
你可能在许多列中有NaN值,请尝试进行子集选择:
df.where(college_grad_mask).dropna(subset=['educ1']).head()
或者更好的方法是:
df[college_grad_mask].head()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论