In this python pandas program, we will change the order of the index of a given series using pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a series using pd.Series().
- Change the order of the index of a given series using df.reindex(index=[2,3,0,1,4,5]).
- It will arrange the series in the above index order.
- Print the output.
import pandas as pd
df = pd.Series(data = ['sqa','tools','learning','python','is','fun'],
index = [0,1,2,3,4,5])
print("Original Data Series:")
print(df)
df = df.reindex(index = [2,3,0,1,4,5])
print("Data Series after changing the order of index:")
print(df)
Output :
Original Data Series:
0 sqa
1 tools
2 learning
3 python
4 is
5 fun
dtype: object
Data Series after changing the order of index:
2 learning
3 python
0 sqa
1 tools
4 is
5 fun
dtype: object