How to Reorder Columns in Pandas: Various Methods

reseting index of the dataframe

Pandas is a python package that allows you to create dataframe from the datasets. This it makes very easy to manipulate them using the various inbuilt functions provided by this package. Suppose you have pandas dataframe and wants to reorder columns then how you will do so? In this tutorial, you will learn the various ways to reorder columns in pandas.

Methods on How to reorder columns in pandas

Let’s know all the methods to reorder columns in pandas.

Method 1: Using the DataFrame.reindex method

It returns the dataframe with the new index. The syntax of this function is below.

DataFrame.reindex(labels=None, index=None, columns=None, axis=None, method=None, 
copy=None, level=None, fill_value=nan, limit=None, tolerance=None)

Here you will pass the index as the list of columns that will be the new index.

Execute the below lines of code to reorder the columns of the dataframe.

Sample Dataframe

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df
 name age country
0 Rob 23 USA
1 Bam 25 UK
2 Maya 26 France
3 Rahul 32 Germany

Reindex columns

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df.reindex(columns=["country","name","age"])

Output

Reindexing column using reindex function
Reindexing column using reindex function

Method 2: Reorder columns in pandas using the DataFrame.columns

You can use the dataframe.columns to give the new name for the existing columns in the dataframe. But make sure that the no of columns name should match the number of columns in the existing dataframe.

Run the below lines of code to reorder the column.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df.columns= ["Name","Age","Country"]
df

Output

changing the name of the columns
changing the name of the columns

Method 3: Using the DataFrame.sort_index

It returns the original DataFrame sorted by the labels or None if passed inplace=True.

Run the below lines of code.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data,index=["c","b","a","d"])
df.sort_index()

Output

sorting dataframe on index
sorting dataframe on index

Method 4: Using DataFrame.reset_index

It will reset the existing index for the dataframe. This allows you to choose the new index for the dataframe from the existing column names.

Run the below lines of code to choose the new index for the dataframe.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data,index=["a","b","c","d"])
df.reset_index()

Output

reseting index of the dataframe
reseting index of the dataframe

Conclusion

Sometimes it requires reordering the columns in dataframe for simplicity. For example, if you have datetime columns and it is not the index column then you can make it using the reset_index() column. If you want to reorder the columns then the above methods will work for you.

I hope you have liked this tutorial. If you have any questions then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner