What will be the output of the following code?
seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
seriesCapCntry[1:3]
What will be the output of the following code?
seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
seriesCapCntry[1:3]
Solution
Integer slicing on a Series is positional, like a Python list: takes positions 1 and 2, and stops before position 3. Position 0 is India, so the slice returns USA and UK together with their index labels.
USA WashingtonDC
UK London
dtype: object
Slicing with labels works differently: seriesCapCntry['USA':'UK'] gives the same result here, but the end label is included.
Option 2: a Series always prints its index labels along with the values, so the labels cannot be left out.
Option 3: this would need the end position to be included, but with position 3 (France) is excluded.
Option 4: this starts from position 0 (India), but the slice starts at position 1.
Related questions:
2026: 26 May Shift 1