Skip to main contentSkip to solution

Choose the correct output of the following Python code:

import pandas as pd

import numpy as np

series1 = pd.Series(np.arange(10,16,1),index = ['a', 'b', 'c', 'd', 'e', 'f'])

print(series1[2:4])

Solution

✅ Correct Option: 1

np.arange(10, 16, 1) gives the values 10, 11, 12, 13, 14, 15, which are labelled a to f. Slicing with integers, as in series1[2:4], is positional, like a list slice. It starts at position 2 and stops before position 4, so it returns positions 2 and 3: c 12 and d 13.


Option 2: this is what a label-based slice such as series1['c':'e'] would give, because label slices include the end label. Integer slices exclude the end.

Positions start from 0, so position 2 is label c, not b.

Keyboard Shortcuts

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