Filtering on multiple conditions
So far, you've selectively imported records that met a single condition, but it's also common to filter datasets on multiple criteria. In this exercise, you'll do just that.
The weather
table contains daily high and low temperatures and precipitation amounts for New York City. Let's focus on inclement weather, where there was either an inch or more of snow or the high was at or below freezing (32° Fahrenheit). To do this, you'll need to build a query that uses the OR
operator to look at values in both columns.
pandas
is loaded as pd
, and a database engine, engine
, has been created.
This exercise is part of the course
Streamlined Data Ingestion with pandas
Exercise instructions
- Create a query that selects records in
weather
wheretmax
is less than or equal to 32 degreesOR
snow
is greater than or equal to 1 inch. - Use
read_sql()
to query the database and assign the result to the variablewintry_days
. - View summary statistics with the
describe()
method to make sure all records in the dataframe meet the given criteria.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create query for records with max temps <= 32 or snow >= 1
query = """
SELECT *
FROM weather
____ ____
____ ____;
"""
# Query database and assign result to wintry_days
wintry_days = pd.read_sql(____)
# View summary stats about the temperatures
print(____)