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']
}
df = pd.DataFrame(data)
print(df)

Output:

                    Name  Shares Symbol
0  Microsoft Corporation     100   MSFT
1            Google, LLC      50   GOOG
2            Tesla, Inc.     150   TSLA
3             Apple Inc.     200   AAPL
4          Netflix, Inc.      80   NFLX

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.

df_new = df[['Name', 'Symbol', 'Shares']]
print(df_new)

Output:

                    Name Symbol  Shares
0  Microsoft Corporation   MSFT     100
1            Google, LLC   GOOG      50
2            Tesla, Inc.   TSLA     150
3             Apple Inc.   AAPL     200
4          Netflix, Inc.   NFLX      80

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.

df_new = df.loc[:, ['Name', 'Symbol', 'Shares']]
print(df_new)

Output:

                    Name Symbol  Shares
0  Microsoft Corporation   MSFT     100
1            Google, LLC   GOOG      50
2            Tesla, Inc.   TSLA     150
3             Apple Inc.   AAPL     200
4          Netflix, Inc.   NFLX      80

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.

df_new = df.iloc[:, [0, 2, 1]]
print(df_new)

Output:

                    Name Symbol  Shares
0  Microsoft Corporation   MSFT     100
1            Google, LLC   GOOG      50
2            Tesla, Inc.   TSLA     150
3             Apple Inc.   AAPL     200
4          Netflix, Inc.   NFLX      80

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.