Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Series
What DataFrames are
DataFrame columns
DataFrame rows
Filtering rows and columns
Filtering data frames
26. Filtering rows – multiple conditions
Sorting rows
Summary

Instruction

Great! We can also use multiple conditions when filtering our DataFrames with the help of the logical operators AND (&) and OR (|).

hospitals[(hospitals['State'] == 'WA') | (hospitals['State'] == 'PA')]

The expression above will show only those hospitals that are either in Pennsylvania (PA) or Washington (WA). Note that each condition is wrapped in parentheses.

Usually, it is more readable to assign each condition to a particular variable, for the sake of clarity. Suppose we want to get hospitals that are in South Carolina with a rating of 4 or better. We could write:

sc_hospitals = hospitals['State'] == 'SC'
good_hospitals = hospitals['Hospital overall rating'] > 3
hospitals[sc_hospitals & good_hospitals]

Exercise

For every country in the eu_states DataFrame, show only those countries that use the Euro as their main currency, and were accepted into European Union before the year 2000.

The information about join date is in the Accession Year column.

Stuck? Here's a hint!

You can create two helper variables, each holding a given condition:

accession_year = eu_states['Accession Year'] < 2000
currency = eu_states['Currency'] == 'Euro'

Then, simply filter by these two variables:

eu_states[accession_year & currency]