How to change column order in Pandas DataFrame in Python

You are at the perfect place if you want to learn functions for how to swap column order in Pandas DataFrame in Python. There are different functions available for swapping the column order in Pandas DataFrame. In this tutorial few of the functions in Python have been discussed below that can perform our task. Follow this tutorial carefully to explore new methods and enhance your knowledge.

Pandas DataFrame

Pandas is a library in Python that is utilized for the study of data in Python programming. An example of a Pandas DataFrame is given below.

# import the pandas library
import pandas as pd
# data
a = {'Student':['Harry', 'John', 'Dolf', 'Ziggler'],
     'Age':['27', '24', '22', '32'],
     'Country':['India', 'Australia', 'England', 'South Africa'],
     'Course':['Python','Data Structures','Machine Learning','Web Development'],
     'Marks':['85','72','89','76']}
# pandas DataFrame
df = pd.DataFrame(a)
# Print the DataFrame
print(df)

Output:

Kindly, make sure your system has Pandas library installed in it or you may install it using the command below:

pip install pandas

Column Order

The column order of the DataFrame can be swapped using the following functions.

loc[] :

This function helps alter the columns by putting the columns in your preferred sequence as shown below.

# import the pandas library
import pandas as pd
# data
a = {'Student':['Harry', 'John', 'Dolf', 'Ziggler'],
     'Age':['27', '24', '22', '32'],
     'Country':['India', 'Australia', 'England', 'South Africa'],
     'Course':['Python','Data Structures','Machine Learning','Web Development'],
     'Marks':['85','72','89','76']}
# Pandas DataFrame
df = pd.DataFrame(a)
# This function allows you to alter the sequence accordingly
df1=df.loc[:,['Course','Marks','Age','Student','Country']]
# Print the DataFrame with the altered columns
print(df1)

Output:

iloc[] :

This function swaps the columns by arranging the index of the columns in the preferred sequence as shown below.

# import the pandas library
import pandas as pd
# data
a = {'Student':['Harry', 'John', 'Dolf', 'Ziggler'],
     'Age':['27', '24', '22', '32'],
     'Country':['India', 'Australia', 'England', 'South Africa'],
     'Course':['Python','Data Structures','Machine Learning','Web Development'],
     'Marks':['85','72','89','76']}
# Pandas DataFrame
df = pd.DataFrame(a)
# alter the sequence using the index location
df1=df.iloc[:,[4,0,3,2,1]]
# Print the DataFrame with the altered columns
print(df1)

Output:

cols.reverse() :

You can reverse the sequence of the columns using this function as given below.

# import the pandas library
import pandas as pd
# data
a = {'Student':['Harry', 'John', 'Dolf', 'Ziggler'],
     'Age':['27', '24', '22', '32'],
     'Country':['India', 'Australia', 'England', 'South Africa'],
     'Course':['Python','Data Structures','Machine Learning','Web Development'],
     'Marks':['85','72','89','76']}
# Pandas DataFrame
df = pd.DataFrame(a)
# assign the variable cols the value of columns of the DataFrame in the form of a list
cols=list(df.columns)
# reverse the sequence
cols.reverse()
# print the reversed sequence
print(df[cols])

Output:

Making a list of columns in a particular sequence :

In this method, you can place the columns in any order in a list and this way you can alter the sequence of the columns.

# import the pandas library
import pandas as pd
# data
a = {'Student':['Harry', 'John', 'Dolf', 'Ziggler'],
     'Age':['27', '24', '22', '32'],
     'Country':['India', 'Australia', 'England', 'South Africa'],
     'Course':['Python','Data Structures','Machine Learning','Web Development'],
     'Marks':['85','72','89','76']}
# Pandas DataFrame
df = pd.DataFrame(a)
# make a list of the columns accordingly
df=df[['Marks','Age','Country','Course','Student']]
# print the DataFrame
print(df)

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *