Reorder Pandas Columns: Pandas Reindex and Pandas insert

Reorder pandas columns cover image

In this post, you’ll learn the different ways to reorder Pandas columns, including how to use the .reindex() method to reorder Pandas columns.

Loading a sample dataframe

Let’s start things off by loading a sample dataframe that you can use throughout the entire tutorial. We’ll only need to import Pandas for this tutorial:

import pandas as pd

df = pd.DataFrame.from_dict(
    {
        'Name': ['Joan', 'Devi', 'Melissa', 'Dave'],
        'Age':[19, 43, 27, 32],
        'Gender': ['Female', 'Female', 'Female', 'Male'],
        'Education': ['High School', 'College', 'PhD', 'High School'],
        'City': ['Atlanta', 'Toronto', 'New York City', 'Madrid']
    }
)

print(df)

This returns the following dataframe:

      Name  Age  Gender    Education           City
0     Joan   19  Female  High School        Atlanta
1     Devi   43  Female      College        Toronto
2  Melissa   27  Female          PhD  New York City
3     Dave   32    Male  High School         Madrid

Reorder Columns by Direct Assignment

The most direct way to reorder columns is by direct assignment (pardon the pun!).

What this means is to place columns in the order that you’d like them to be in as a list, and pass that into square brackets when re-assigning your dataframe.

Right now, the dataframe’s columns are in the following order: [‘Name’, ‘Age’, ‘Gender’, ‘Education’, ‘City’].

Imagine that you want to switch out the Age and Gender columns, you could write:

df = df[['Name', 'Gender', 'Age', 'Education', 'City']]

print(df)

This returns the following dataframe, with its columns reordered:

      Name  Gender  Age    Education           City
0     Joan  Female   19  High School        Atlanta
1     Devi  Female   43      College        Toronto
2  Melissa  Female   27          PhD  New York City
3     Dave    Male   32  High School         Madrid

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Reorder Columns using Pandas .reindex()

Another way to reorder columns is to use the Pandas .reindex() method. This allows you to pass in the columns= parameter to pass in the order of columns that you want to use.

For the following example, let’s switch the Education and City columns:

df = df.reindex(columns=['Name', 'Gender', 'Age', 'City', 'Education'])

print(df)

This returns the following dataframe:

      Name  Gender  Age           City    Education
0     Joan  Female   19        Atlanta  High School
1     Devi  Female   43        Toronto      College
2  Melissa  Female   27  New York City          PhD
3     Dave    Male   32         Madrid  High School

Reorder Pandas Columns using Pandas .insert()

Both of the above methods rely on your to manually type in the list of columns. If you’re working with a larger dataframe, this can be time consuming and just, plain, annoying!

If you know the position in which you want to insert the column, this is a much easier way to do so.

Let’s check out how to do this using Python. For the following example, let’s move the City column between Name and Gender (as the second column).

city = df['City']
df = df.drop(columns=['City'])
df.insert(loc=1, column='City', value=city)

print(df)

Let’s take a quick look at what we’ve done here:

  1. We’ve assigned the df[‘City’] column to a series name city,
  2. We dropped that column from the dataframe, and finally
  3. We inserted that series back into the dataframe, at the first index with a column named ‘City’

This returns the following dataframe:

      Name           City  Gender  Age    Education
0     Joan        Atlanta  Female   19  High School
1     Devi        Toronto  Female   43      College
2  Melissa  New York City  Female   27          PhD
3     Dave         Madrid    Male   32  High School

Reorder Columns using a Custom Function

We can use the above method as a custom function, if you find yourself needing to move multiple columns around more frequently.

Let’s see how we do this with Python:

def reorder_columns(dataframe, col_name, position):
    """Reorder a dataframe's column.

    Args:
        dataframe (pd.DataFrame): dataframe to use
        col_name (string): column name to move
        position (0-indexed position): where to relocate column to

    Returns:
        pd.DataFrame: re-assigned dataframe
    """
    temp_col = dataframe[col_name]
    dataframe = dataframe.drop(columns=[col_name])
    dataframe.insert(loc=position, column=col_name, value=temp_col)

    return dataframe

df = reorder_columns(dataframe=df, col_name='Age', position=0)

print(df)

This returns the following dataframe:

   Age     Name           City  Gender    Education
0   19     Joan        Atlanta  Female  High School
1   43     Devi        Toronto  Female      College
2   27  Melissa  New York City  Female          PhD
3   32     Dave         Madrid    Male  High School

Conclusion

In this post, you learned how to reorder columns, including how to use the Pandas .reindex() method and the .insert() method. In the end, you learned a custom function to help you reorder columns, if this is something you do frequently.

If you want to learn more about the Pandas .reindex()method, check out the official documentation here.