Challenge - 5 Problems
Series Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of selecting elements with mixed index types
What is the output of this code snippet?
Data Analysis Python
import pandas as pd s = pd.Series([10, 20, 30, 40], index=['a', 1, 'b', 2]) result = s.loc[[1, 'b']] print(result)
Attempts:
2 left
💡 Hint
Remember that .loc uses label-based indexing and can handle mixed types in the index.
✗ Incorrect
The Series has mixed index labels: 'a', 1, 'b', 2. Using .loc with [1, 'b'] selects the elements with labels 1 and 'b', which correspond to values 20 and 30.
❓ data_output
intermediateNumber of elements selected by slicing with .iloc
How many elements are selected by this code?
Data Analysis Python
import pandas as pd s = pd.Series(range(10), index=list('abcdefghij')) subset = s.iloc[3:7] print(len(subset))
Attempts:
2 left
💡 Hint
Remember that slicing with iloc is end-exclusive.
✗ Incorrect
The slice 3:7 selects elements at positions 3,4,5,6, which is 4 elements.
🔧 Debug
advancedIdentify the error in this Series selection code
What error does this code raise?
Data Analysis Python
import pandas as pd s = pd.Series([1,2,3], index=[0,1,2]) result = s.loc[1:3] print(result)
Attempts:
2 left
💡 Hint
.loc slicing with labels is inclusive of the stop label.
✗ Incorrect
Using .loc with slice 1:3 selects labels 1 and 2 because label 3 does not exist, but slicing stops at the last valid label before 3.
🧠 Conceptual
advancedUnderstanding difference between .loc and .iloc with duplicate indices
Given a Series with duplicate indices, which statement is true about .loc and .iloc?
Attempts:
2 left
💡 Hint
Think about label-based vs position-based selection.
✗ Incorrect
.loc selects all rows with the given label, even if duplicated. .iloc selects rows by integer position, ignoring labels.
🚀 Application
expertSelect elements conditionally using Series indexing
Given the Series below, which code snippet correctly selects all elements with values greater than 15 and index labels that are strings?
Data Analysis Python
import pandas as pd s = pd.Series([10, 20, 30, 40], index=['a', 1, 'b', 2])
Attempts:
2 left
💡 Hint
Use .map with a function to check index types and combine conditions properly.
✗ Incorrect
Option C correctly uses .map with a lambda to check if index labels are strings and combines it with the value condition. Options A and C compare arrays incorrectly, and D uses .loc with a boolean index which is valid but the condition in D is same as B but .loc is not needed here.
