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

This community-built FAQ covers the “Select Rows with Logic II” 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 II

You can help contribute to this section by offering your own questions or feedback on the exercise!

Unlock your understanding, and share that aha moment with someone else! Teaching others and answering their questions is one of the best ways to learn.

Questions about formatting, syntax, or conceptual understanding are just a few avenues you may consider exploring.

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!

In this exercise, we use operators (&, |) instead of the operators learned in Python 3 (and, or) - why do we shift the syntax?


Not sure if you found the information, but I found a post on StackOverflow that’s quite helpful.
Link: https://stackoverflow.com/questions/21415661/logical-operators-for-boolean-indexing-in-pandas

TL;DR : Basically, the Pandas Objects, i.e. Series, don’t have an inherent boolean value. A variable can either hold data or not (aka the None value); this means it’s truthy if it has data or falsy if it’s null. The Pandas Objects hold lots of data, so differentiating what makes these objects True or False becomes arbitrary. The Pandas creators are letting you decide what is true or false by requiring you to compare the obj’s elements versus the entire obj.

1 Like

Why do both type brackets (() or ) work for the expressions below?
march_april = df[[df.month==‘March’] | [df.month==‘April’]]
march_april = df[(df.month==‘March’) | (df.month==‘April’)]


My keyboard doesn’t seem to have a bar symbol for the Or operant. How do I type the or symbol?


How do you type in the Bar symbol which stands for the “Or” operator if it’s not on your keyboard? The lesson won’t actually let you proceed without doing so. I had to click the get solution button to move forward but what I’m actually worried about is having to use it irl.

Hmm. Are you sure the correct key, |, isn’t located near your backspace? That’s where it is on my keyboard.

Do not understand the use of the operator or ‘|’ instead of and ‘&’ in the command: march_april = df[(df.month ==
‘March’) | (df.month == ‘April’)]

Seems to me if one wants to display two rows the ‘&’ and operator should be used. But when the and ‘&’ operator is used, it displays only the column headers.

When we use AND (&) we want to see rows where the first statement ((df.month == ‘April’) and the second (df.month ==
‘March’) is TRUE together. Such a ROW must contain ‘April’ and ‘March’ at the same time, but it’s impossible. As a result - it displays only the column headers.
When we use OR (|) we want to see rows where ‘month’ is April OR March. There is such rows.