📖 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
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
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
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
Hint
Use print(filtered_products) to show the filtered rows.
Practice
(1/5)
1. What does the isin() function do in pandas?
easy
A. Checks if each value in a Series or DataFrame is in a given list and returns True or False.
B. Sorts the values in a DataFrame or Series.
C. Calculates the sum of values in a DataFrame column.
D. Removes duplicate values from a DataFrame.
Solution
Step 1: Understand the purpose of isin()
isin() checks if each element in a pandas Series or DataFrame exists in a list or another iterable.
Step 2: Identify the output type
The function returns a boolean Series or DataFrame with True where the value is found and False otherwise.
Final Answer:
Checks if each value in a Series or DataFrame is in a given list and returns True or False. -> Option A
Quick Check:
isin() = membership check [OK]
Hint: Remember: isin() returns True/False for membership [OK]
Common Mistakes:
Thinking isin() sorts data
Confusing isin() with aggregation functions
Assuming it removes duplicates
2. Which of the following is the correct syntax to filter rows in a DataFrame df where column col has values in the list [1, 2, 3]?
easy
A. df.isin(col, [1, 2, 3])
B. df.col.isin([1, 2, 3]) = True
C. df[df.isin(col, [1, 2, 3])]
D. df[df.col.isin([1, 2, 3])]
Solution
Step 1: Use isin() on the column
The correct way is to call isin() on the Series: df.col.isin([1, 2, 3]).
Step 2: Use boolean indexing to filter rows
Wrap the boolean Series inside df[ ... ] to select rows where condition is True.
Final Answer:
df[df.col.isin([1, 2, 3])] -> Option D
Quick Check:
Filter rows with boolean mask = df[df.col.isin([1, 2, 3])] [OK]
Hint: Call isin() on column, then filter with df[...] [OK]
A. The DataFrame df must have a column named 'mask' to filter.
B. The isin() method is called on the whole DataFrame, resulting in a DataFrame of booleans, which cannot be used directly for filtering.
C. The variable mask should be converted to a list before filtering.
D. The list passed to isin() must be a string, not integers.
Solution
Step 1: Understand df.isin([1, 3]) output
This returns a DataFrame of booleans with the same shape as df, not a single boolean Series.
Step 2: Why filtering with DataFrame mask fails
Using a DataFrame of booleans as an index mask causes an error because pandas expects a boolean Series or array matching the index length.
Final Answer:
The isin() method is called on the whole DataFrame, resulting in a DataFrame of booleans, which cannot be used directly for filtering. -> Option B
Quick Check:
Mask must be Series, not DataFrame [OK]
Hint: Use isin() on a column, not whole DataFrame for filtering [OK]
Common Mistakes:
Using DataFrame mask instead of Series mask for filtering
Passing wrong data types to isin()
Trying to filter with a DataFrame of booleans
5. You have a DataFrame df with a column status containing values like 'new', 'pending', 'done', and 'cancelled'. You want to create a new column is_active that is True only if status is 'new' or 'pending'. Which code correctly does this using isin()?
hard
A. df['is_active'] = df['status'] == ['new', 'pending']
B. df['is_active'] = df.isin(['new', 'pending'])
C. df['is_active'] = df['status'].isin(['new', 'pending'])
D. df['is_active'] = df['status'].isin('new', 'pending')
Solution
Step 1: Use isin() on the 'status' column with a list of target values
Correct syntax is df['status'].isin(['new', 'pending']) which returns a boolean Series.
Step 2: Assign the boolean Series to new column 'is_active'
This creates a new column with True where status is 'new' or 'pending', False otherwise.
Final Answer:
df['is_active'] = df['status'].isin(['new', 'pending']) -> Option C
Quick Check:
Use isin() on column with list [OK]
Hint: Assign isin() result directly to new column [OK]
Common Mistakes:
Calling isin() on whole DataFrame
Passing multiple arguments instead of a list
Using equality operator with list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.