Reorder or Rearrange the column of dataframe in pandas python

Reordering or Rearranging the column of dataframe in pandas python can be done by using reindex function. In order to reorder or rearrange the column in pandas python. We will be different methods. To reorder the column in ascending order we will be using Sort() function. To reorder the column in descending order we will be using Sort function with an argument reverse =True. let’s get clarity with an example.

  • Re arrange or re order the column of dataframe in pandas python with example
  • Re arrange the column of the dataframe by column name.
  • Re arrange the column of the dataframe by column position.
  • Reorder the column in python in ascending order
  • Reorder the column in python in descending order

First let’s create a dataframe

##### Create a DataFrame

import pandas as pd
import numpy as np


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

##### Reorder the column of dataframe in pandas python

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

 

 

Rearrange the column of dataframe by column name in pandas python

Reorder or rearrange the column of dataframe  by column name in pandas python can be done by following method

##### Rearrange the column of dataframe by column name in pandas python

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

so the resultant dataframe will be

Reorder or Rearrange column in Pandas Python 3

 

 

Rearrange the column of dataframe by column position in pandas python

Reorder or rearrange the column of dataframe by column position in pandas python can be done by following method

##### Rearrange the column of dataframe by column position in pandas python

df2=df1[df1.columns[[3,2,1,0]]]
print(df2)

so the resultant dataframe will be

Reorder or Rearrange column in Pandas Python 4a

 

 

Reorder the column of dataframe by ascending order in pandas python

Reorder the column of dataframe by ascending order in pandas python can be done by following method

  • First Get the list of column names
  • Sort the list of column names in ascending order
  • Reorder the column by passing the sorted column names

As shown below

###### Reorder the column of dataframe by ascending order in pandas 

cols=df1.columns.tolist()
cols.sort()
df2=df1[cols]
print(df2)

so the resultant dataframe will be

Reorder or Rearrange column in Pandas Python 5

 

 

Reorder the column of dataframe by descending order in pandas python

Reorder the column of dataframe by descending order in pandas python can be done by following method

  • First Get the list of column names
  • Sort the list of column names in descending order
  • Reorder the column by passing the sorted column names

As shown below

#####  Reorder the column of dataframe by descending order in pandas 

cols=df1.columns.tolist()
cols.sort(reverse=True)
df2=df1[cols]
print(df2)

so the resultant dataframe will be

Reorder or Rearrange column in Pandas Python 6

 

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

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