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])
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
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.
Related questions:
2025: 3 June Shift 2
2026: 26 May Shift 1
2025: 13 May Shift 2
2025: 20 May Shift 1