Bird
Raised Fist0
Data Analysis Pythondata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does series['a'] do in a pandas Series with index labels?
easy
A. Returns a slice from start to label 'a'
B. Selects the value at label 'a' in the Series
C. Selects the value at position 0 in the Series
D. Raises an error because 'a' is not a position

Solution

  1. Step 1: Understand label-based indexing

    Using series['a'] accesses the value at the index label 'a', not position.
  2. Step 2: Differentiate label vs position

    Label 'a' is a named index, so it returns the value at that label.
  3. Final Answer:

    Selects the value at label 'a' in the Series -> Option B
  4. Quick Check:

    Label indexing = value at label [OK]
Hint: Use labels inside quotes to get values by label [OK]
Common Mistakes:
  • Confusing label with position indexing
  • Thinking 'a' is a position index
  • Expecting a slice instead of single value
2. Which of these is the correct syntax to select multiple labels 'a' and 'c' from a Series named ser?
easy
A. ser.loc[['a', 'c']]
B. ser['a', 'c']
C. ser.loc['a', 'c']
D. ser.iloc[['a', 'c']]

Solution

  1. Step 1: Understand selection of multiple labels

    To select multiple labels, use loc with a list of labels inside double brackets.
  2. Step 2: Check syntax correctness

    ser.loc[['a', 'c']] is correct; distractors B and C use tuples (KeyError), D uses iloc which expects positions (TypeError).
  3. Final Answer:

    ser.loc[['a', 'c']] -> Option A
  4. Quick Check:

    Use loc with list for multiple labels [OK]
Hint: Use ser.loc[[labels]] to select multiple labels [OK]
Common Mistakes:
  • Using single brackets for multiple labels
  • Omitting loc when selecting multiple labels
  • Passing labels as separate arguments instead of a list
3. Given the Series:
import pandas as pd
ser = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
print(ser['b':'d'])

What is the output?
medium
A. b 20 c 30 d 40 dtype: int64
B. b 20 c 30 dtype: int64
C. 20 30 40
D. 20 30

Solution

  1. Step 1: Understand label slicing in Series

    Label slicing with ser['b':'d'] includes both start and end labels.
  2. Step 2: Identify values for labels 'b' to 'd'

    Labels 'b', 'c', and 'd' correspond to values 20, 30, and 40 respectively.
  3. Final Answer:

    b 20 c 30 d 40 dtype: int64 -> Option A
  4. Quick Check:

    Label slice includes end label [OK]
Hint: Label slices include the end label [OK]
Common Mistakes:
  • Excluding the end label in slice
  • Confusing label slicing with position slicing
  • Expecting only values without index labels
4. What is wrong with this code snippet?
import pandas as pd
ser = pd.Series([1, 2, 3], index=['x', 'y', 'z'])
print(ser[1:2])

Why might this cause confusion?
medium
A. It raises a KeyError because 1 and 3 are not labels
B. It returns a single value instead of a slice
C. It returns values for labels 'y' and 'z' inclusive
D. It slices by position, excluding index label 'z'

Solution

  1. Step 1: Understand position slicing in Series

    Using ser[1:2] slices by position, not label, and excludes the end position.
  2. Step 2: Identify slice range and output

    Position 1 (inclusive) to 2 (exclusive) corresponds to label 'y' (position 1), excluding position 2 ('z').
  3. Final Answer:

    It slices by position, excluding index label 'z' -> Option D
  4. Quick Check:

    Position slices exclude end position [OK]
Hint: Position slices exclude the end index [OK]
Common Mistakes:
  • Expecting label slicing with integer slice
  • Thinking slice includes end position
  • Confusing KeyError with slicing behavior
5. You have a Series ser with index labels ['a', 'b', 'c', 'd', 'e'] and values [5, 10, 15, 20, 25]. How do you select all values greater than 10 using Series indexing and selection?
hard
A. ser[ser.index > 10]
B. ser.index[ser > 10]
C. ser[ser > 10]
D. ser.loc[ser.index > 10]

Solution

  1. Step 1: Use boolean indexing on Series values

    Comparing ser > 10 creates a boolean mask for values greater than 10.
  2. Step 2: Apply boolean mask to Series

    Using ser[ser > 10] selects values where the mask is True.
  3. Final Answer:

    ser[ser > 10] -> Option C
  4. Quick Check:

    Boolean mask on values selects correct subset [OK]
Hint: Use ser[ser > value] to filter by value condition [OK]
Common Mistakes:
  • Trying to filter by index values instead of data values
  • Using loc with boolean mask on index instead of values
  • Confusing index labels with values in condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard