Skip to content
Advertisement

List comprehension with multiple conditions on different columns

I have the following df,

JavaScript

& would like to get the total number of Male who either Agree or Disagree. I expect the answer to be 3 but instead get 9.

JavaScript

I have done this through other methods and I’m trying to understand list comprehension better.

Advertisement

Answer

Let’s unpack this a bit. The original statement

JavaScript

is equivalent to

JavaScript

I think it should be clear in this case why you get 9.

What you actually want is to only consider corresponding pairs of two equal sized iterables. There’s the handy zip built-in in python which does just this,

JavaScript

or as a comprehension

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement