Using DataFrame.set_axis() method in Pandas

Updated: February 20, 2024 By: Guest Contributor Post a comment

Introduction

The set_axis() method in Pandas is a powerful way to assign new labels to either the index (row labels) or columns of a DataFrame. It offers a greater degree of control for data manipulation and is essential when working with datasets that need reindexing or renaming of axes for better clarity or for subsequent operations.

In this tutorial, we will cover the basics of the set_axis() method, followed by progressive examples that demonstrate its versatility, from basic to advanced usage. By the end of this guide, you will have a solid understanding of how to effectively use set_axis() in your data preprocessing workflow.

Getting Started with set_axis()

First, ensure you have Pandas installed in your Python environment. If not, you can do so by running:

pip install pandas

Let’s first create a simple DataFrame for our examples:

import pandas as pd

data = pd.DataFrame({
    'A': [1,2,3],
    'B': [4,5,6],
    'C': [7,8,9]
})

print(data)

This will output:

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

The first example involves renaming column labels using set_axis(). To do this, we simply assign a new list of labels to the columns parameter:

data.set_axis(['X', 'Y', 'Z'], axis=1, inplace=True)
print(data)

This modifies the original DataFrame columns from ‘A’, ‘B’, ‘C’ to ‘X’, ‘Y’, ‘Z’:

   X  Y  Z
0  1  4  7
1  2  5  8
2  3  6  9

Now, let’s look at renaming row labels. This time, we assign a new list of labels to the index parameter:

data.set_axis(['One', 'Two', 'Three'], axis=0, inplace=True)
print(data)

Our DataFrame now has custom row labels:

     X  Y  Z
One  1  4  7
Two  2  5  8
Three  3  6  9

Advanced Examples

As we dive deeper, let’s explore more complex manipulations. One such scenario involves using set_axis() in conjunction with other methods for more dynamic reindexing operations. Suppose you want to update the column labels based on some operation, like appending a suffix:

new_columns = [col + '_new' for col in data.columns]
data.set_axis(new_columns, axis='columns', inplace=True)
print(data)

This changes the column headers to include a suffix, showcasing the method’s ability to integrate with list comprehensions and other Python functionalities:

       X_new  Y_new  Z_new
One       1      4      7
Two       2      5      8
Three     3      6      9

Another advanced use case is when working with multi-level indices (hierarchical data). Assume our DataFrame now has a two-level index:

data.index = pd.MultiIndex.from_tuples([('One', 'a'), ('Two', 'b'), ('Three', 'c')], names=['Outer', 'Inner'])
data.set_axis(['Alpha', 'Beta', 'Gamma'], axis='columns', inplace=True)
print(data)

This demonstrates how set_axis() can be effectively used to rename columns in a multi-level indexed DataFrame, enhancing readability and accessibility of the dataset:

         Alpha  Beta  Gamma
Outer Inner                 
One   a        1     4      7
Two   b        2     5      8
Three c        3     6      9

Conclusion

Throughout this tutorial, we’ve explored the set_axis() method in Pandas, from its basic to advanced applications. Whether renaming columns or indexes, handling singular or multi-level structures, set_axis() provides a robust solution for modifying DataFrame axes. By incorporating these techniques into your data manipulation toolkit, you can bring clarity and precision to your datasets.