Skip to Content
Learn
Creating, Loading, and Selecting Data with Pandas
Select Rows with Logic II

You can also combine multiple logical statements, as long as each statement is in parentheses.

For instance, suppose we wanted to select all rows where the customer’s age was under 30 or the customer’s name was “Martha Jones”:

name address phone age
Martha Jones 123 Main St. 234-567-8910 28
Rose Tyler 456 Maple Ave. 212-867-5309 22
Donna Noble 789 Broadway 949-123-4567 35
Amy Pond 98 West End Ave. 646-555-1234 29
Clara Oswald 54 Columbus Ave. 714-225-1957 31

We could use the following code:

df[(df.age < 30) | (df.name == 'Martha Jones')]

In Python, | means “or” and & means “and”.

Instructions

1.

You want to see how the number of clinic visits changed between March and April.

Create the variable march_april, which contains the data from March and April. Do this using two logical statements combined using |, which means “or”.

2.

Inspect march_april using print.

Folder Icon

Sign up to start coding

Already have an account?