How to swap two DataFrame columns in Pandas

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
6 min

banner

# Table of Contents

  1. How to swap two DataFrame columns in Pandas
  2. Swapping column names and values in a DataFrame
  3. Using a reusable function to swap two DataFrame columns in Pandas

# How to swap two DataFrame columns in Pandas

To swap two DataFrame columns in Pandas:

  1. Store the reordered column names in a list.
  2. Use the DataFrame.reindex() method to swap the DataFrame columns.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'age': [29, 30, 31, 32], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) column_names = ['name', 'salary', 'age'] df = df.reindex(columns=column_names) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name age salary 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4 -------------------------------------------------- name salary age 0 Alice 175.1 29 1 Bobby 180.2 30 2 Carl 190.3 31 3 Dan 205.4 32

swap two dataframe columns in pandas

Note: the example only changes the order of the columns in the DataFrame. If you also want to change the column contents, click on the following subheading:

The DataFrame.reindex() method conforms the DataFrame to the new index.

main.py
column_names = ['name', 'salary', 'age'] df = df.reindex(columns=column_names)

The columns argument is an array-like object that stores the new labels for the columns.

You can also achieve the same result by using the loc indexer.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'age': [29, 30, 31, 32], 'salary': [175.1, 180.2, 190.3, 205.4], }) column_names = list(df) print(column_names) # ๐Ÿ‘‰๏ธ ['name', 'age', 'salary'] column_names[1], column_names[2] = column_names[2], column_names[1] print('-' * 50) print(column_names) # ๐Ÿ‘‰๏ธ ['name', 'salary', 'age'] df = df.loc[:, column_names] print('-' * 50) # name salary age # 0 Alice 175.1 29 # 1 Bobby 180.2 30 # 2 Carl 190.3 31 # 3 Dan 205.4 32 print(df)
The code for this article is available on GitHub

We used the list() class to get a list containing the column names.

The next step is to swap the columns using their respective indices.

main.py
column_names[1], column_names[2] = column_names[2], column_names[1]

Lastly, we use the df.loc indexer to set the updated column names.

main.py
df = df.loc[:, column_names]

# Swapping column names and values in a DataFrame

The two previous examples only change the order of the DataFrame columns.

If you also need to change the contents of the columns, set the updated columns list using the columns attribute.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [29, 30, 31, 32], 'age': [175.1, 180.2, 190.3, 205.4], }) print(df) column_names = list(df) column_names[1], column_names[2] = column_names[2], column_names[1] df.columns = column_names print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary age 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4 -------------------------------------------------- name age salary 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4

swap two dataframe columns and their contents

We used the list() class to get a list of the names of the columns in the DataFrame.

main.py
# ๐Ÿ‘‡๏ธ ['name', 'salary', 'age'] column_names = list(df)

The next step is to reorder the column names in the list.

main.py
column_names = list(df) # ๐Ÿ‘‡๏ธ ['name', 'salary', 'age'] print(column_names) column_names[1], column_names[2] = column_names[2], column_names[1] # ๐Ÿ‘‡๏ธ ['name', 'age', 'salary'] print(column_names)

Once the order is correct, use the DataFrame.columns attribute to swap the two columns.

This approach swaps the names of the columns and their contents as opposed to the previous two (which only swapped the column names).

# Using a reusable function to swap two DataFrame columns in Pandas

If you have a large DataFrame with many columns, it might be easier to define a reusable function that swaps the two columns.

main.py
import pandas as pd def swap_df_columns(df, col1, col2): col_list = list(df) a, b = col_list.index(col1), col_list.index(col2) col_list[b], col_list[a] = col_list[a], col_list[b] df = df[col_list] return df df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'age': [29, 30, 31, 32], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) df = swap_df_columns(df, 'salary', 'age') print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name age salary 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4 -------------------------------------------------- name salary age 0 Alice 175.1 29 1 Bobby 180.2 30 2 Carl 190.3 31 3 Dan 205.4 32

swap two dataframe columns using reusable function

The function does all the heavy lifting for us.

It takes the DataFrame and the names of the columns as arguments.

main.py
def swap_df_columns(df, col1, col2): col_list = list(df) a, b = col_list.index(col1), col_list.index(col2) col_list[b], col_list[a] = col_list[a], col_list[b] df = df[col_list] return df

The function creates a list of the column names of the DataFrame.

Then, the list.index() method is used to get the index of the supplied column names.

The column names are swapped using the indices.

The last step is to update the DataFrame and return the result.

We could've also used the loc indexer to achieve the same result.

main.py
import pandas as pd def swap_df_columns(df, col1, col2): col_list = list(df) a, b = col_list.index(col1), col_list.index(col2) col_list[b], col_list[a] = col_list[a], col_list[b] # โœ… Using loc indexer instead df = df.loc[:, col_list] return df df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'age': [29, 30, 31, 32], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) df = swap_df_columns(df, 'salary', 'age') print('-' * 50) print(df)
The code for this article is available on GitHub

The code sample produces the same output.

shell
name age salary 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4 -------------------------------------------------- name salary age 0 Alice 175.1 29 1 Bobby 180.2 30 2 Carl 190.3 31 3 Dan 205.4 32

You can slightly tweak the function if you need to also swap the contents of the columns (and not only the names).

main.py
import pandas as pd def swap_df_columns(df, col1, col2): col_list = list(df) a, b = col_list.index(col1), col_list.index(col2) col_list[b], col_list[a] = col_list[a], col_list[b] # โœ… Swap contents of columns as well df.columns = col_list return df df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [29, 30, 31, 32], 'age': [175.1, 180.2, 190.3, 205.4], }) print(df) df = swap_df_columns(df, 'salary', 'age') print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary age 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4 -------------------------------------------------- name age salary 0 Alice 29 175.1 1 Bobby 30 180.2 2 Carl 31 190.3 3 Dan 32 205.4

also swap contents of columns in dataframe

We used the DataFrame.columns attribute to swap the names and contents of the two columns.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev