Skip to Content

Change Order of Columns of a Pandas DataFrame

During the data preprocessing and feature creation stage, it might happen that you end up with columns that may not necessarily be in the order that you’d like. In this tutorial, we’ll look at how to change the order of columns of a pandas dataframe.

To change the order of columns of a dataframe, you can pass a list with columns in the desired order to [] (that is, indexing with []). The following is the syntax:

df_correct_order = df[[col1, col2, col3, ..., coln]]

Generally, we use [] in Pandas dataframes to subset a dataframe but it can also be used to reorder the columns. You can also use .loc and .iloc to change the order of columns of a dataframe.

First, let’s create a dataframe that we’ll be using throughout this tutorial.

import pandas as pd

data = {
    'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\
             'Apple Inc.', 'Netflix, Inc.'],
    'Shares': [100, 50, 150, 200, 80],
    'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX']
}

# create dataframe
df = pd.DataFrame(data)
# display the dataframe
df

Output:

Pandas dataframe with 5 rows and 3 columns containing information on a stock portfolio.

Here, df is a dataframe of a sample stock portfolio with columns Name, Shares, Symbol. We want to reorder the columns such that the resulting dataframe has columns in the order Name, Symbol, Shares. Let’s see examples of some of the ways we can achieve this.

As mentioned above, you can pass the columns in the order you like as a list.

# new dataframe with different column order
df_new = df[['Name', 'Symbol', 'Shares']]
# display the dataframe
df_new

Output:

pandas dataframe after changing column order

In the above example, we change the order of columns from Name, Shares, Symbol to Name, Symbol, Shares.

You can also reorder a pandas dataframe by indexing it using .loc. This way, you can reorder columns using their names as we did in the previous example.

# new dataframe with different column order
df_new = df.loc[:, ['Name', 'Symbol', 'Shares']]
# display the dataframe
df_new

Output:

pandas dataframe after changing column order

In the above example, we change the order of columns from Name, Shares, Symbol in the original dataframe df to Name, Symbol, Shares in the returned dataframe df_new using the dataframe’s .loc property.

You can also change the column order of a dataframe by indexing it using .iloc. Here, we pass the column indexes instead of their names in the order that we want.

# new dataframe with different column order
df_new = df.iloc[:, [0, 2, 1]]
# display the dataframe
df_new

Output:

pandas dataframe after changing column order

In the above example, we reorder columns from the original dataframe df using their indexes rather than their names. The resulting dataframe is saved as df_new. The columns in the dataframe df_new are ordered Name, Symbol, Shares.

For more, refer to pandas’ official guide on indexing and selecting data.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.