0
0
Data Analysis Pythondata~20 mins

Series indexing and selection in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Series Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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)
AKeyError: '[1, 'b'] not found in axis'
B
a     10
b     30
dtype: int64
C
1    20
2    40
dtype: int64
D
1     20
b     30
dtype: int64
Attempts:
2 left
💡 Hint
Remember that .loc uses label-based indexing and can handle mixed types in the index.
data_output
intermediate
1:30remaining
Number 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))
A3
B4
C5
D7
Attempts:
2 left
💡 Hint
Remember that slicing with iloc is end-exclusive.
🔧 Debug
advanced
2:00remaining
Identify 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)
ANo error, outputs elements with labels 1 and 2
BIndexError: index out of range
CTypeError: unhashable type
DKeyError: 3
Attempts:
2 left
💡 Hint
.loc slicing with labels is inclusive of the stop label.
🧠 Conceptual
advanced
2:30remaining
Understanding difference between .loc and .iloc with duplicate indices
Given a Series with duplicate indices, which statement is true about .loc and .iloc?
A.loc returns all rows matching the label, .iloc returns rows by position regardless of duplicates
B.loc returns only the first matching label, .iloc returns all positions of that label
C.loc and .iloc behave identically with duplicate indices
D.loc raises an error if duplicates exist, .iloc does not
Attempts:
2 left
💡 Hint
Think about label-based vs position-based selection.
🚀 Application
expert
3:00remaining
Select 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])
As[(s > 15) & (s.index.map(type) == str)]
Bs.loc[(s > 15) & (s.index.map(type) == str)]
Cs[(s > 15) & (s.index.map(lambda x: isinstance(x, str)))]
Ds.loc[(s > 15) & (s.index.map(lambda x: isinstance(x, str)))]
Attempts:
2 left
💡 Hint
Use .map with a function to check index types and combine conditions properly.