Different methods to change the order of columns in pandas DataFrame
In this tutorial we will discuss how to change the order of columns in panads dataframe using the following methods:
- Using
reindex()
function - Using
sort_index()
function - Using
indexing
- Move columns to particular
position
Create pandas DataFrame with example data
DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.
We can create the DataFrame by using pandas.DataFrame() method.
Syntax:
pandas.DataFrame(input_data,columns,index)
Parameters:
It will take mainly three parameters
- input_data is represents a list of data
columns
represent the columns names for the dataindex
represent the row numbers/values
We can also create a DataFrame using dictionary by skipping columns and indices.
Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe
print(dataframe)
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
You can learn more at Pandas dataframe explained with simple examples
Method 1 : Using reindex() function
reindex()
function is used to change the order of columns in the given dataframe.
It will take list of specified columns as input and return the dataframe according to the specified order.
Syntax:
dataframe.reindex(columns=[list_of_columns])
where,
- dataframe is the input dataframe
- columns takes the
list of columns
specified in an order
Example 1: Define the order of individual columns
In this example we are going to order the dataframe.
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe aafter ordering columns
print(dataframe.reindex(columns=['cost','quantity','id','name']))
Output:
cost quantity id name
item-1 567.00 1 foo-23 ground-nut oil
item-2 562.56 2 foo-13 almonds
item-3 67.00 3 foo-02 flour
item-4 76.09 2 foo-31 cereals
Example 2: Order the dataframe columns alphabetically
If we want to order the columns in an alphabetical order, then we have to use sorted()
method with reindex()
function.
Syntax:
dataframe.reindex(sorted(dataframe.columns), axis=1)
Example: In this example, we are going to order the columns in alphabetical order
.
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns in alphabetical order
print(dataframe.reindex(sorted(dataframe.columns), axis=1))
Output:
cost id name quantity
item-1 567.00 foo-23 ground-nut oil 1
item-2 562.56 foo-13 almonds 2
item-3 67.00 foo-02 flour 3
item-4 76.09 foo-31 cereals 2
Method 2 : Using sort_index() function
sort_index()
function is used to order the columns based on ascending or descending order
.So we can change the order of columns by using this fuction.
Syntax:
dataframe.sort_index(axis=1, ascending)
where,
- dataframe is the input dataframe
axis=1
specifies columnascending
is the parameter used to order the columns in ascending order when it is set toTrue
and order the dataframe columns indescending
order when it is set toFalse
Example 1: Chage the order of dataframe columns in ascending order
In this example we will change the order of columns in ascending order
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns in ascending order
print(dataframe.sort_index(axis=1, ascending=True))
Output:
cost id name quantity
item-1 567.00 foo-23 ground-nut oil 1
item-2 562.56 foo-13 almonds 2
item-3 67.00 foo-02 flour 3
item-4 76.09 foo-31 cereals 2
Example 2: Change the order of columns in descending order
In this example we will change the order of columns in descending order
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns in descending order
print(dataframe.sort_index(axis=1, ascending=False))
Output:
quantity name id cost
item-1 1 ground-nut oil foo-23 567.00
item-2 2 almonds foo-13 562.56
item-3 3 flour foo-02 67.00
item-4 2 cereals foo-31 76.09
Method 3 : Using indexing
In this method , we are going change the order of columns by passing the columns in a list in an order through index - []
.
Syntax:
dataframe[[list_of_columns]]
where,
- dataframe is the input dataframe
list_of_columns
is the columns in an order
Example 1: In this example, we specified the columns through an index - [] in the order - ['cost','id','name','quantity']
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns
print(dataframe[['cost','id','name','quantity']])
Output:
cost id name quantity
item-1 567.00 foo-23 ground-nut oil 1
item-2 562.56 foo-13 almonds 2
item-3 67.00 foo-02 flour 3
item-4 76.09 foo-31 cereals 2
Example 2: In this example, we specified the columns through an index - [] in the order - ['name','quantity','cost','id']
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns
print(dataframe[['name','quantity','cost','id']])
Output:
name quantity cost id
item-1 ground-nut oil 1 567.00 foo-23
item-2 almonds 2 562.56 foo-13
item-3 flour 3 67.00 foo-02
item-4 cereals 2 76.09 foo-31
Method 4 : Move columns to particular position
Here we are going to order the columns by changing the position
.
So first we have to remove the column at the position and then we replace the column at particular column.
We can remove by using pop()
function
Syntax:
column = dataframe.pop('column_name')
where, column_name is the column to be replaced
We can insert the column by using insert()
function
Syntax:
dataframe.insert(position, 'column_name', column)
where, position
is an index value to place the column at given position
Example 1: Program to remove cost column and place at first position
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns
# by moving cost column to first
column = dataframe.pop('cost')
dataframe.insert(0, 'cost', column)
# display dataframe
print(dataframe)
Output:
cost id name quantity
item-1 567.00 foo-23 ground-nut oil 1
item-2 562.56 foo-13 almonds 2
item-3 67.00 foo-02 flour 3
item-4 76.09 foo-31 cereals 2
Example 2: Program to remove id column and place at last position
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe after ordering columns
# by moving id column to last
column = dataframe.pop('id')
dataframe.insert(3, 'id', column)
# display dataframe
print(dataframe)
Output:
name cost quantity id
item-1 ground-nut oil 567.00 1 foo-23
item-2 almonds 562.56 2 foo-13
item-3 flour 67.00 3 foo-02
item-4 cereals 76.09 2 foo-31
Summary
In this article, we discussed how to change the order of columns in pandas dataframe using reindex(), sort_index() , indexing
methods, we also seen that how to order the columns alphabetically using sorted()
with reindex()
function.
pop()
and insert()
functions played an important role to change the order of columns.
References