0
0
Pandasdata~15 mins

isin() for value matching in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using isin() for Value Matching in pandas
📖 Scenario: You work in a store and have a list of products with their categories. You want to find which products belong to certain categories you are interested in.
🎯 Goal: Build a pandas DataFrame with product data, create a list of categories to filter, use isin() to find matching products, and display the filtered results.
📋 What You'll Learn
Create a pandas DataFrame called products with columns 'Product' and 'Category' using exact data.
Create a list called selected_categories with exact category names.
Use isin() on the 'Category' column of products to filter rows.
Print the filtered DataFrame.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to filter product lists by categories to analyze or display specific groups.
💼 Career
Data analysts and scientists use <code>isin()</code> to quickly filter data based on lists of values, which is common in data cleaning and exploration.
Progress0 / 4 steps
1
Create the products DataFrame
Create a pandas DataFrame called products with two columns: 'Product' and 'Category'. Use these exact entries: 'Apple' in 'Fruit', 'Carrot' in 'Vegetable', 'Banana' in 'Fruit', 'Broccoli' in 'Vegetable', and 'Chicken' in 'Meat'.
Pandas
Need a hint?

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

2
Create the list of selected categories
Create a list called selected_categories with these exact values: 'Fruit' and 'Meat'.
Pandas
Need a hint?

Use square brackets to create a list with the exact category names.

3
Filter products using isin()
Create a new DataFrame called filtered_products by selecting rows from products where the 'Category' column values are in the list selected_categories. Use the isin() method on the 'Category' column.
Pandas
Need a hint?

Use products['Category'].isin(selected_categories) inside the square brackets to filter rows.

4
Print the filtered products
Print the DataFrame filtered_products to display the products that belong to the selected categories.
Pandas
Need a hint?

Use print(filtered_products) to show the filtered rows.