pandas DataFrame 查询在使用 where 时不起作用。

huangapple go评论78阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月1日 12:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定