Skip to main contentSkip to solution

What would be correct syntax to select elements at positions 0 and 2 from a Series 's'.

Solution

✅ Correct Option: 2

Passing a list of labels inside .loc selects exactly those entries, so s.loc[[0,2]] returns the elements with labels 0 and 2. With the default integer index, these are the elements at positions 0 and 2.


Option 1: s.loc[0,2] is read as a tuple key, as if it were (row, column). A Series has only one axis, so this raises an error instead of selecting two elements.

Option 3: s.iloc[0:2] is a slice that excludes the end, so it returns positions 0 and 1, not 0 and 2.

Option 4: s[0,2] also passes a tuple, not a list, so it fails. Two or more items must be wrapped in a list: s[[0,2]].

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question