What will be output of the following python code?
import pandas as pd
series1 = pd.Series(["Kavi","Shyam","Ravi"], index=[3,5,1])
print(series1[::-1])
What will be output of the following python code?
import pandas as pd
series1 = pd.Series(["Kavi","Shyam","Ravi"], index=[3,5,1])
print(series1[::-1])
Solution
The slice [::-1] reverses the order of the Series. Each index label stays attached to its own value, so the pairs 3-Kavi, 5-Shyam and 1-Ravi are listed from last to first.
The output is:
1 Ravi
5 Shyam
3 Kavi
dtype: object
Option 2: it reverses the values but also moves the labels, giving wrong pairs such as 3-Shyam and 5-Kavi.
Option 4: it drops the index, but a Series always prints its index labels.
Slicing with [::-1] never changes the label-value pairs, only their order. Real pandas output also ends with the line dtype: object.
Related questions:
2026: 31 May Shift 1
2025: 3 June Shift 2
2025: 15 May Shift 2