FAQ: Creating, Loading, and Selecting Data with Pandas - Select Rows with Logic III

This community-built FAQ covers the “Select Rows with Logic III” exercise from the lesson “Creating, Loading, and Selecting Data with Pandas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise Select Rows with Logic III

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m confused about the ‘or’ (using this symbol: | ) logic in using multiple logical statements to select rows. Here’s an example from the exercise:

march_april = df[(df.month == ‘March’) | (df.month == ‘April’)]

The idea of this exercise was to select the two rows that contained data from march and april to make a smaller DataFrame. Why do we use the or symbol (|) instead of and (&)? If I hadn’t done this lesson, it would appear to me that this piece of code was going to pick march or april from the dataset, and not both? But the lesson is telling us that it’s picking both with an or command?

blegh. i don’t understand, this seems to be breaking all the rules of or/and logic.

This is quite late but I’ll explain the reason for this incase anybody else comes along.

The reason we use OR here is because we want data where in the month column we have either the value March or April.

If we were to use AND we would be looking for data where in the month column we have simultaneously the value March and April.

AND is typically suited for when we stacking different attributes say we want to look at data in the month of April AND the clinic east being 81.

Hopefully this distinction is helpful.