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

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

  • Re arrange or re order the column 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 column of dataframe in pandas python 1

 

Reorder the column of dataframe in pandas python

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


df2=df1.reindex(columns= ['Rounded_score', 'Gender', 'Score','Name'])
print(df2)

so the resultant dataframe will be

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

 

p Re arrange or Re order the column of dataframe in pandas python                                                                                                         n Re arrange or Re order the column of dataframe in pandas python