Consider the given series:
seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
Select the output of the following command:
seriesCapCntry[ : : -1]
Consider the given series:
seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
Select the output of the following command:
seriesCapCntry[ : : -1]
Solution
The slice has no start or stop and a step of , so it reverses the whole series. The elements are read from the last to the first, and each index label stays with its value.
| Index | Value |
|---|---|
| France | Paris |
| UK | London |
| USA | WashingtonDC |
| India | NewDelhi |
Options 1 and 3: these drop India (and France), which would need a start or stop value in the slice, such as or .
Option 4: this shows only two items, as if the slice were . With no start or stop, all four items are returned.
A negative step in a pandas Series slice works just like it does in a Python list: always reverses.
Related questions:
2025: 3 June Shift 2
2025: 3 June Shift 2
2026: 23 May Shift 1