0
0
Pandasdata~20 mins

isin() for value matching in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Isin Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A[True, False, True, False, True]
B[True, True, False, True, False]
C[False, False, False, False, False]
D[False, True, False, True, False]
Attempts:
2 left
💡 Hint
Check which values in the series are exactly in the list [20, 40, 60].
data_output
intermediate
2: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)
A
   Name     City
0  Anna       NY
2  Cathy      NY
3  David  Chicago
B
   Name     City
1  Bob        LA
3  David  Chicago
C
   Name     City
0  Anna       NY
1  Bob        LA
D
   Name     City
2  Cathy      NY
3  David  Chicago
Attempts:
2 left
💡 Hint
Look for rows where City is either 'NY' or 'Chicago'.
🧠 Conceptual
advanced
2: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())
A[True, True, True]
B[False, False, False]
C[]
D[None, None, None]
Attempts:
2 left
💡 Hint
Think about what it means to check if values are in an empty list.
🔧 Debug
advanced
2: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)
ATypeError: argument of type 'int' is not iterable
BValueError: wrong number of arguments
CAttributeError: 'int' object has no attribute 'isin'
DNo error, outputs [False, True, False]
Attempts:
2 left
💡 Hint
Check what type the argument to isin() should be.
🚀 Application
expert
3: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)
A5
B4
C3
D2
Attempts:
2 left
💡 Hint
Check rows where Category is 'A' or 'C' and Score is 10 or 30 simultaneously.