0
0
Pandasdata~30 mins

query() for fast filtering in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Sales Data Using pandas query()
📖 Scenario: You work in a store and have a table of sales data. You want to quickly find sales that meet certain conditions, like sales above a certain amount or sales from a specific product.
🎯 Goal: Build a small program that uses pandas query() to filter sales data based on a condition.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Create a variable for the sales amount threshold
Use query() to filter rows where sales are above the threshold
Print the filtered DataFrame
💡 Why This Matters
🌍 Real World
Filtering data quickly helps businesses analyze sales trends and make decisions fast.
💼 Career
Data analysts and scientists often use pandas <code>query()</code> to filter large datasets efficiently.
Progress0 / 4 steps
1
Create the sales DataFrame
Import pandas as pd and create a DataFrame called sales with these exact columns and rows:
Product: 'Apple', 'Banana', 'Carrot', 'Date', 'Eggplant'
Amount: 50, 30, 20, 40, 10
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Set the sales amount threshold
Create a variable called threshold and set it to 25 to filter sales above this amount.
Pandas
Need a hint?

Just assign the number 25 to a variable named threshold.

3
Filter sales using query()
Use query() on the sales DataFrame to create a new DataFrame called filtered_sales that only has rows where Amount is greater than threshold.
Pandas
Need a hint?

Use sales.query('Amount > @threshold') to filter rows where Amount is greater than threshold. The @ symbol lets you use the variable threshold inside the query string.

4
Print the filtered sales
Print the filtered_sales DataFrame to see the rows where sales amount is greater than 25.
Pandas
Need a hint?

Use print(filtered_sales) to show the filtered rows.