df = pd. Series(['London', 'Paris', 'NewDelhi', 'Moscow'], index = ['UK', 'France', 'India', 'Russia'])
Which of the following would reverse the order of the series?
df = pd. Series(['London', 'Paris', 'NewDelhi', 'Moscow'], index = ['UK', 'France', 'India', 'Russia'])
Which of the following would reverse the order of the series?
Solution
The slice df[::-1] uses a step of -1 with the start and stop left blank. This walks through the Series from the last element to the first, giving Russia (Moscow), India (NewDelhi), France (Paris), UK (London). The index labels reverse along with the values.
Option 1: df[:-1] keeps the order and only drops the last element.
Option 2: df[-1:-3] has a positive step, so it goes from the end towards the end, and the result is an empty Series.
Option 4: df[3::1] starts at position 3 and moves forward, returning only the last element (Moscow).
In slicing, a negative step reverses the direction, so [::-1] is the standard way to reverse a Series, list or string.
Related questions:
2025: 30 May Shift 2
2026: 31 May Shift 1