Change the order of the index of a given series

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
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Change the order of the index of a given series using df.reindex(index=[2,3,0,1,4,5]).
  4. It will arrange the series in the above index order.
  5. 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
				
			

create a subset of a given series based on value and condition

find the mean of the data of a given series

Leave a Comment