gpt4 book ai didi

python - 仅适用第一个条件

转载 作者:行者123 更新时间:2023-12-01 03:30:47 27 4
gpt4 key购买 nike

我是Python新手。我想要进行以下过滤,但仅应用第一个条件,而忽略其他条件。你能告诉我我错在哪里吗?这是我的数据: enter image description here

import pandas as pd
census_df = pd.read_csv('census.csv')
census_df.head()
census_df

val1=census_df['POPESTIMATE2015']
val2=census_df['POPESTIMATE2014']

def answer_one():

return census_df[val1>val2 & (census_df['REGION']==1 | (census_df['REGION']==2) & (census_df['CTYNAME'].str.len=='Washington') ) ]

answer_one()

最佳答案

我认为你需要知道这一点(Python operators precedence):

**          Exponentiation (raise to the power)
~ + - Ccomplement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'td>
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators

对于您的情况:

census_df[(val1 > val2) & ((census_df['REGION']==1) | 
(census_df['REGION']==2)) &
(census_df['CTYNAME']=='Washington')]

可能是您正在寻找的内容?

关于python - 仅适用第一个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com