【发布时间】:2022-01-23 17:20:56
【问题描述】:
我有以下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' ]
我已经通过其他方法做到了这一点,我正在尝试更好地理解列表理解。
【问题讨论】:
-
这能回答你的问题吗? How to iterate through two lists in parallel? 我知道它说的是“列表”,但对于系列来说也是一样的。
标签: python pandas list-comprehension