In this article we will discuss different ways to Change the Order of Columns in Pandas DataFrame.
Table Of Contents
- Change the order of Dataframe Columns using reindex()
- Change the order of Dataframe Columns using sort_index()
- Change the order of Dataframe Columns using Indexing
- Change the order by moving specific column to particular position
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s create a dataframe with 4 rows and 4 columns
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'id' :[7058,7069,7060,7061], 'name':['sravan','bobby','ojaswi','deepu'], 'age' :[21,23,22,21], 'subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df)
Output:
id name age subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql
Change the order of Dataframe Columns using reindex()
In Pandas, the Dataframe provides a function reindex(). It allows us to change the index positions of the columns or rows. We are going to us this to change the order of columns in the Dataframe. Syntax is as follows,
Frequently Asked:
df.reindex(columns=list_of_column_labels)
where, df is the input dataframe and in the columns parameter, we can pass a list of column labels separated by comma. Based on this arrangement of column labels in list, the columns of Dataframe will get re-arranged.
Rearrange columns using reindex()
Here we are going to pass a list of column labels in the columns parameter.
Example: We are going to arrange the columns in the specified order.
- Subjects
- Roll_Number
- Name
- Age
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Change the order of columns based on column names in list df = df.reindex(columns=['Subjects','Roll_Number','Name','Age']) # Display The Dataframe print(df)
Output:
Latest Python - Video Tutorial
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Subjects Roll_Number Name Age 0 linux 7058 sravan 21 1 html/css 7069 bobby 23 2 node-js 7060 ojaswi 22 3 php-mysql 7061 deepu 21
Rearrange columns alphabetically using reindex()
Create a list of sorted column labels and pass that to the reindex() function. If you don’t want to use the columns parameter, then pass the list as first argument and axis=1. It will change the order of columns based on the column names in the list.
Syntax is as follows,
df.reindex(sorted(df.columns), axis=1)
where, df is the input dataframe and parameters are,
- sorted(df.columns) : A sorted list of column labels
- axis =1 specifies the column.
As we are passing a sorted list of column labels, so dataframe columns will be arranged in alphabetical order. For example,
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Change the order of columns in alphabetical order df = df.reindex(sorted(df.columns), axis=1) # Display The Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Age Name Roll_Number Subjects 0 21 sravan 7058 linux 1 23 bobby 7069 html/css 2 22 ojaswi 7060 node-js 3 21 deepu 7061 php-mysql
Change the order of Dataframe Columns using sort_index()
In Pandas, the Dataframe provides a function sort_index (). It helps to sort the Dataframe by rows or columns in an ascending or descending order. we can use that to rearrange the columns of dataframe by column names in ascending or descending order. Syntax is as follows,
Syntax:
df.sort_index(axis=1,ascending)
where, df is the input dataframe and parameters are:
- axis=1 specifies the column axis
- ascending is used to order the columns in ascending order when it is set to True, otherwise it will order the columns in descending order when it is set to False. By default it is ascending.
Change the columns order in ascending order using sort_index() method
Here we are going to change the order of the dataframe columns to ascending order. For example,
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Change the order of columns and rearragne them in ascending order df = df.sort_index(axis=1) # Display The Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Age Name Roll_Number Subjects 0 21 sravan 7058 linux 1 23 bobby 7069 html/css 2 22 ojaswi 7060 node-js 3 21 deepu 7061 php-mysql
Change the columns order in descending order using sort_index() method
Here we are going to change the order of the dataframe columns to descending order. For example:
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Change the order of columns and rearragne them in descending order df = df.sort_index(axis=1, ascending=False) # Display The Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Subjects Roll_Number Name Age 0 linux 7058 sravan 21 1 html/css 7069 bobby 23 2 node-js 7060 ojaswi 22 3 php-mysql 7061 deepu 21
Change Order of Columns in Dataframe using Indexing
By using Indexing operator – [], we can change the order of columns. We can do that by passing a list of rearranged column names in the index operator. Syntax is as follow,
df[[list_of_column_names]]
where, df is the input dataframe. Inside the index operator, we passed a list of column names. It will rearrange the columns in specified order. Let’s see an example,
Here , we will change the order the columns to: ‘Subjects’, ‘Roll_Number’, ‘Name’, ‘Age’
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Change the order of columns using Indexing df = df[['Subjects','Roll_Number','Name','Age']] # Display The Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Subjects Roll_Number Name Age 0 linux 7058 sravan 21 1 html/css 7069 bobby 23 2 node-js 7060 ojaswi 22 3 php-mysql 7061 deepu 21
Change the order by moving specific column to particular position
We can change the order of columns, by moving a particular column based on it’s index position through pop() and insert() methods.
First we have to pop the moving column. Then, bBy using insert() method we have to insert that popped element in the position. Here postion is based on the column index. (index starts with 0). Syntax is as follows:
column = df.pop('column_name') df.insert(position, 'column_name', column)
Example: Here , we will move Subjects column to the first position
import pandas as pd # Create the dataframe with four columns df = pd.DataFrame({'Roll_Number' :[7058,7069,7060,7061], 'Name':['sravan','bobby','ojaswi','deepu'], 'Age' :[21,23,22,21], 'Subjects':['linux','html/css','node-js','php-mysql']}) # Display The Dataframe print(df) # Move 'Subjects' column to 1st position column = df.pop('Subjects') df.insert(0, 'Subjects', column) # Display The Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql Subjects Roll_Number Name Age 0 linux 7058 sravan 21 1 html/css 7069 bobby 23 2 node-js 7060 ojaswi 22 3 php-mysql 7061 deepu 21
Summary
In this article, we discussed several ways like reindex(), sort_index(), indexing to change the order of the columns in Pandas DataFrame.
Latest Video Tutorials