Challenge - 5 Problems
Isin Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of isin() with multiple values
What is the output of this code snippet?
Pandas
import pandas as pd s = pd.Series([10, 20, 30, 40, 50]) result = s.isin([20, 40, 60]) print(result.tolist())
Attempts:
2 left
💡 Hint
Check which values in the series are exactly in the list [20, 40, 60].
✗ Incorrect
The isin() method returns True for elements that are in the given list. Here, 20 and 40 are in the list, so their positions are True; others are False.
❓ data_output
intermediate2:00remaining
Filtering DataFrame rows using isin()
Given the DataFrame below, which rows remain after filtering with isin()?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Anna', 'Bob', 'Cathy', 'David'], 'City': ['NY', 'LA', 'NY', 'Chicago'] }) filtered = df[df['City'].isin(['NY', 'Chicago'])] print(filtered)
Attempts:
2 left
💡 Hint
Look for rows where City is either 'NY' or 'Chicago'.
✗ Incorrect
Rows with City 'NY' or 'Chicago' are kept. That includes Anna, Cathy, and David.
🧠 Conceptual
advanced2:00remaining
Behavior of isin() with empty list
What is the output of this code snippet?
Pandas
import pandas as pd s = pd.Series([1, 2, 3]) result = s.isin([]) print(result.tolist())
Attempts:
2 left
💡 Hint
Think about what it means to check if values are in an empty list.
✗ Incorrect
No values can be in an empty list, so all results are False.
🔧 Debug
advanced2:00remaining
Error caused by wrong argument type in isin()
What error does this code raise?
Pandas
import pandas as pd s = pd.Series([1, 2, 3]) result = s.isin(2) print(result)
Attempts:
2 left
💡 Hint
Check what type the argument to isin() should be.
✗ Incorrect
isin() expects an iterable like list or set. Passing an int causes a TypeError because int is not iterable.
🚀 Application
expert3:00remaining
Count how many DataFrame rows match multiple conditions using isin()
Given this DataFrame, how many rows have 'Category' in ['A', 'C'] and 'Score' in [10, 30]?
Pandas
import pandas as pd df = pd.DataFrame({ 'Category': ['A', 'B', 'C', 'A', 'B', 'C'], 'Score': [10, 20, 30, 40, 10, 30] }) filtered = df[df['Category'].isin(['A', 'C']) & df['Score'].isin([10, 30])] count = filtered.shape[0] print(count)
Attempts:
2 left
💡 Hint
Check rows where Category is 'A' or 'C' and Score is 10 or 30 simultaneously.
✗ Incorrect
Rows matching both conditions are: (A,10), (C,30), and (C,30) again. Total 3 rows.