4 Ways to Change the Column Order of a Pandas Dataframe in Python

Python Change The Column Order Of A Pandas DataFrame

In this tutorial, we are going to discuss how we can change the column order of a given pandas DataFrame object. During the data preprocessing stage, we might encounter a situation where the columns of the concerned pandas DataFrame are not in the desired order then we have to change the column order of the DataFrame.

Also read: Pandas DataFrame Indexing: Set the Index of a Pandas Dataframe


How to change the column order of a Pandas Dataframe?

Let’s get right into the different methods to change the column order of a dataframe in Pandas.

Method 1: Using the desired order columns list

This is one of the simplest methods to change the order of the columns of a pandas DataFrame object. In this method, we simply pass the Python list of columns of the DataFrame in the desired order to the DataFrame object. Let’s see how to code this method in Python.

# Method-1

# Import pandas Python module
import pandas as pd

# Create a pandas DataFrame
df = pd.DataFrame({'Roll': [111, 112, 113, 114, 115],
                    'Name': ['Sanjay', 'Aman', 'Ankit', 'Ravi', 'Komal'],
                    'Branch': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
                    'CGPA': [8.15, 9.03, 7.85, 8.55, 9.45]})
print('Given pandas DataFrame:\n')
print(df)

# Change the order of the DataFrame
# Using the desired order columns list
df_1 = df[['Name', 'CGPA', 'Roll', 'Branch']]
print('\nPandas DataFrame with changed column order:\n')
print(df_1)

Output:

Given pandas DataFrame:

   Roll    Name Branch  CGPA
0   111  Sanjay    ECE  8.15
1   112    Aman    ICE  9.03
2   113   Ankit     IT  7.85
3   114    Ravi    CSE  8.55
4   115   Komal    CHE  9.45

Pandas DataFrame with changed column order:

     Name  CGPA  Roll Branch
0  Sanjay  8.15   111    ECE
1    Aman  9.03   112    ICE
2   Ankit  7.85   113     IT
3    Ravi  8.55   114    CSE
4   Komal  9.45   115    CHE

Method 2: Using the loc method

In this method, we will make use of the loc method of the pandas DataFrame class. Using the loc method, we can reorder the columns of the pandas DataFrame object by providing a Python list of column names. Let’s write the Python code to implement this method.

# Method-2

# Import pandas Python module
import pandas as pd

# Create a pandas DataFrame
df = pd.DataFrame({'Name': ['Sanjay', 'Aman', 'Ankit', 'Ravi', 'Komal'],
                    'Roll': [111, 112, 113, 114, 115],
                    'Branch': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
                    'CGPA': [8.15, 9.03, 7.85, 8.55, 9.45]})
print('Given pandas DataFrame:\n')
print(df)

# Change the order of the DataFrame
# Using the loc method of pandas DataFrame class
df_2 = df.loc[2:4, ['Roll', 'Name', 'CGPA', 'Branch']]
print('\nPandas DataFrame with changed column order:\n')
print(df_2)

Output:

Given pandas DataFrame:

     Name  Roll Branch  CGPA
0  Sanjay   111    ECE  8.15
1    Aman   112    ICE  9.03
2   Ankit   113     IT  7.85
3    Ravi   114    CSE  8.55
4   Komal   115    CHE  9.45

Pandas DataFrame with changed column order:

   Roll   Name  CGPA Branch
2   113  Ankit  7.85     IT
3   114   Ravi  8.55    CSE
4   115  Komal  9.45    CHE

Method 3: Using the iloc method

In this method, we will be using the iloc method of the pandas DataFrame class. Using the iloc method, we can reorder the columns of the pandas DataFrame object by providing a Python list of column indices (i.e. 0, 1, 2, 3, …) instead of the column names. Let’s see how to implement this method through Python code.

# Method-3

# Import pandas Python module
import pandas as pd

# Create a pandas DataFrame
df = pd.DataFrame({'CGPA': [8.15, 9.03, 7.85, 8.55, 9.45],
                    'Name': ['Sanjay', 'Aman', 'Ankit', 'Ravi', 'Komal'],
                    'Roll': [111, 112, 113, 114, 115],
                    'Branch': ['ECE', 'ICE', 'IT', 'CSE', 'CHE']})
print('Given pandas DataFrame:\n')
print(df)

# Change the order of the DataFrame
# Using the iloc method of pandas DataFrame class
df_3 = df.iloc[1:4, [1, 2, 0, 3]]
print('\nPandas DataFrame with changed column order:\n')
print(df_3)

Output:

Given pandas DataFrame:

   CGPA    Name  Roll Branch
0  8.15  Sanjay   111    ECE
1  9.03    Aman   112    ICE
2  7.85   Ankit   113     IT
3  8.55    Ravi   114    CSE
4  9.45   Komal   115    CHE

Pandas DataFrame with changed column order:

    Name  Roll  CGPA Branch
1   Aman   112  9.03    ICE
2  Ankit   113  7.85     IT
3   Ravi   114  8.55    CSE

NOTE: In the above two methods loc and iloc, we have an added advantage of selecting only a range of rows in the given pandas DataFrame object.

Method 4: Using the reindex() function

In this method, we will be using the reindex() function of the pandas DataFrame object. Using the reindex() function, we can rearrange the columns order of the pandas DataFrame object by passing a Python list of column names. Let’s implement this method through Python code.

# Method-4

# Import pandas Python module
import pandas as pd

# Create a pandas DataFrame
df = pd.DataFrame({'Branch': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
                    'CGPA': [8.15, 9.03, 7.85, 8.55, 9.45],
                    'Name': ['Sanjay', 'Aman', 'Ankit', 'Ravi', 'Komal'],
                    'Roll': [111, 112, 113, 114, 115]})
print('Given pandas DataFrame:\n')
print(df)

# Change the order of the DataFrame
# Using the reindex() function of pandas DataFrame class
df_4 = df.reindex(columns = ['Roll', 'CGPA', 'Name', 'Branch'])
print('\nPandas DataFrame with changed column order:\n')
print(df_4)

Output:

Given pandas DataFrame:

  Branch  CGPA    Name  Roll
0    ECE  8.15  Sanjay   111
1    ICE  9.03    Aman   112
2     IT  7.85   Ankit   113
3    CSE  8.55    Ravi   114
4    CHE  9.45   Komal   115

Pandas DataFrame with changed column order:

   Roll  CGPA    Name Branch
0   111  8.15  Sanjay    ECE
1   112  9.03    Aman    ICE
2   113  7.85   Ankit     IT
3   114  8.55    Ravi    CSE
4   115  9.45   Komal    CHE

Summing-up

In this tutorial, we have learned how the four different ways to change the order of the columns of a pandas DataFrame object. Hope you have understood all the methods discussed above and are excited to use them on your own. Thanks for reading and stay tuned with us for more such amazing content on Python programming.