0
0
Pandasdata~30 mins

Multiple conditions with & and | in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Data with Multiple Conditions Using & and | in pandas
📖 Scenario: You work in a small online store. You have a list of products with their prices and stock counts. You want to find products that meet certain conditions to decide what to promote or reorder.
🎯 Goal: Build a pandas DataFrame with product data, set a price threshold, filter products using multiple conditions combined with & (and) and | (or), and display the filtered results.
📋 What You'll Learn
Create a pandas DataFrame named products with columns Product, Price, and Stock using the exact data provided.
Create a variable price_limit with the exact value 20.
Use multiple conditions combined with & and | to filter products where Price is less than price_limit and Stock is greater than 10, or where Product is exactly 'Notebook'.
Store the filtered DataFrame in a variable named filtered_products.
Print the filtered_products DataFrame.
💡 Why This Matters
🌍 Real World
Filtering data with multiple conditions is common in business to find products that meet specific criteria, like affordable price and enough stock.
💼 Career
Data analysts and scientists often filter datasets using multiple conditions to prepare data for reports, dashboards, or decision-making.
Progress0 / 4 steps
1
Create the products DataFrame
Create a pandas DataFrame called products with these exact columns and data: Product with values ['Pen', 'Notebook', 'Eraser', 'Pencil', 'Marker'], Price with values [5, 25, 3, 2, 15], and Stock with values [50, 5, 100, 20, 0].
Pandas
Need a hint?

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

2
Set the price limit
Create a variable called price_limit and set it to the integer value 20.
Pandas
Need a hint?

Just assign the number 20 to the variable price_limit.

3
Filter products with multiple conditions
Create a variable called filtered_products that filters products where Price is less than price_limit and Stock is greater than 10, or where Product is exactly 'Notebook'. Use parentheses and combine conditions with & and |.
Pandas
Need a hint?

Use parentheses around each condition and combine them with & for AND and | for OR.

4
Print the filtered products
Print the filtered_products DataFrame to display the filtered results.
Pandas
Need a hint?

Use print(filtered_products) to show the filtered DataFrame.