Re arrange or Re order the row of dataframe in pandas python

Re arranging or Re ordering the row of dataframe in pandas python can be done by using reindex function. Let’s see how to

  •  Re arrange or re order the row of dataframe in pandas python with example

First let’s create a dataframe

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'Name':['George','Andrea','micheal','maggie','Ravi',
               'Xien','Jalpa'],
     'Gender':["M","F","M","F","M","M","F"],      
    'Score':[62.7,47.7,55.6,74.6,31.5,77.3,85.4],
    'Rounded_score':[63,48,56,75,32,77,85]
   }


df1 = pd.DataFrame(df1,columns=['Name','Gender','Score','Rounded_score'])
print(df1)

df1 will be

Re arrange or Re order the row of dataframe in pandas python 1

 

Re order the row of dataframe in pandas python

Re ordering or re arranging the row of dataframe in pandas python can be done by using reindex function and stored as new dataframe


df2= df1.reindex([2,1,0,5,6,4,3])
print(df2)

row index of the dataframe is re ordered as shown below

Re arrange or Re order the row of dataframe in pandas python 2

n Re arrange or Re order the row of dataframe in pandas python                                                                                                       n Re arrange or Re order the row of dataframe in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts