如何使用Pandas查询DataFrame并使用多个行条件?

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

how to query dataframe using pandas with multiple row conditions?

问题

You can fix the issue by using parentheses to properly group your conditions in the DataFrame selection. Here's the corrected code:

df_status = df.loc[(df['build'] == 2979) & (df['status'] == 'passed')]

This will return all rows where 'build' is equal to 2979 and 'status' is equal to 'passed'.

英文:

I have the below data set, and I need to use pandas to query the status of build (2979). how many passed and how many failed

     build  status
0     2980  passed
1     2979  passed
2     2979  passed
3     2979  failed
4     2978  failed
..     ...     ...
101   2968  failed
102   2968  passed
103   2968  passed
104   2968  passed
105   2968  failed

if I do:

df_status =df.loc[df['build'] == 2979]

it will return the rows of build == 2979. It is what I wanted
but when I do

df_status =df.loc[df['build'] == 2979]& df['status']=='passed']

it returns TypeError: Cannot perform 'rand_' with a dtyped [object] array and scalar of type [bool].

How do I fix the problem?

I have tried using :

df_status =df.loc[df['build'] == 2979 & df['status']=='passed']

and hope to return all row with build ==2979 and status == 'passed', but it return TypeError: Cannot perform 'rand_' with a dtyped [object] array and scalar of type [bool].

答案1

得分: 1

这是运算符优先级的问题。您忘了用括号括起您的表达式。

当您执行:

df.loc[df['build'] == 2979 & df['status'] == 'passed']

Python会尝试评估:2979 & df['status'],因为&运算符的优先级高于==运算符。

您应该这样做:

df.loc[(df['build'] == 2979) & (df['status'] == 'passed')]

以恢复两个操作的优先级。上面的代码等同于:

df.loc[df['build'].eq(2979) & df['status'].eq('passed')]
英文:

This is problem of operator precedence. You missed to enclose your expression with some parenthesis.

When you do:

df.loc[df['build'] == 2979 & df['status'] == 'passed']

Python try to evaluate: 2979 & df['status'] because & operator has an higher precedence than == operator.

What you should do is:

df.loc[(df['build'] == 2979) & (df['status'] == 'passed')]

To restore the priority of both operations. The code above is equivalent to:

df.loc[df['build'].eq(2979) & df['status'].eq('passed')]

huangapple
  • 本文由 发表于 2023年7月5日 01:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614834.html
匿名

发表评论

匿名网友

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

确定