Reindex in python pandas – change order of row and column in pandas

In this tutorial we will learn how to reindex in python pandas or change the order of the rows and column in python pandas with the help of reindex() function. We will discuss the example for

  • Reindexing or changing the order of Rows in pandas python
  • Reindexing or changing the order of columns in pandas python

Create dataframe:

import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa'],
   'Score1':[62,47,55,74,31,77,85,63,42,32,71,57],
   'Score2':[89,87,67,55,47,72,76,79,44,92,99,69],
   'Score3':[56,86,77,45,73,62,74,89,71,67,97,68]}

df = pd.DataFrame(d)
df

The resultant dataframe will be

change order of column and rows in pandas dataframe 1

 

Reindex or change the order of rows in pandas python:

Now lets change the order of rows as shown below


# reindex or change the order of rows
df.reindex([8,11,9,2, 1, 0,7,5,6,4,10,3])

so the re indexed dataframe will be

change order of column and rows in pandas dataframe 2

 

Reindex or change the order of columns in pandas python:

Now lets change the order of columns as shown below


# reindex or change the order of columns

columnsTitles = ['Score2', 'Score1', 'Score3']
df.reindex(columns=columnsTitles)

so the re indexed dataframe will be

change order of column and rows in pandas dataframe 3

 

previous-small Reindex in python – change order of column and rows in pandas dataframe                                                                                                                next_small Reindex in python – change order of column and rows in pandas dataframe