在不同列上具有多个条件的列表理解

List comprehension with multiple conditions on different columns

我有以下df,

data = [['Male', 'Agree'], ['Male', 'Agree'], ['Male', 'Disagree'], ['Female','Neutral']]
 
df = pd.DataFrame(data, columns = ['Sex', 'Opinion'])
df

& 想要获得同意或不同意的男性总数。我希望答案是 3,但得到的却是 9。

sum([True for x in df['Opinion'] for y in df['Sex'] if x in ['Agree','Disagree'] if y=='Male' ] 

我已经通过其他方法做到了这一点,我正在努力更好地理解列表理解。

让我们稍微解压一下。原文

total = sum([True for x in df['Opinion'] for y in df['Sex'] if x in ['Agree','Disagree'] if y=='Male' ]

相当于

total = 0
for x in df['Opinion']:
    for y in df['Sex']:
        if x in ['Agree', 'Disagree']:
            if y=='Male':
                total += 1

我想在这种情况下应该很清楚为什么你会得到 9

你真正想要的是只考虑对应的两个大小相等的可迭代对象对。 python 中有方便的 zip built-in 可以做到这一点,

total = 0
for x,y in zip(df['Opinion'], df['Sex']):
    if x in ['Agree', 'Disagree'] and y=='Male':
        total += 1

或者作为理解

total = sum(1 for x,y in zip(df['Opinion'], df['Sex']) if x in ['Agree', 'Disagree'] and y=='Male')