Pandas DataFrame set_axis() Method – Explained with Examples

The set_axis() method in Pandas is a powerful tool for renaming the labels of a DataFrame’s axes. This method provides an easy way to rename the rows (index) o0r columns of your DataFrame, making your data more readable and easier to manipulate.

Syntax
Python
DataFrame.set_axis(labels, axis=0)
Parameters
  • labels: list-like
    • The new labels for the specified axis. The length of the labels must match the length of the axis being set.
  • axis: {0 or ‘index’, 1 or ‘columns’}, default 0
    • The axis to update. 0 or 'index' for the index (rows) and 1 or 'columns' for the columns.
  • copy: bool, default True
    • Whether to return a copy of the DataFrame with the updated axis labels. If True, a new DataFrame is returned.
Returns
  • If copy=True, the method returns a new DataFrame with the updated axis labels.
  • If copy=False, the method modifies the DataFrame in place.

Examples

Let’s explore some examples to understand how the set_axis() method works.

Example 1: Rename DataFrame Columns

Suppose we have the following DataFrame:

Python
import pandas as pd

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

df = pd.DataFrame(data)
print(df)

Output:

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

We want to rename the columns to X, Y, and Z.

Python
new_columns = ['X', 'Y', 'Z']
df = df.set_axis(new_columns, axis=1)
print(df)

Output:

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

In this example, by setting axis=1 (or you can also use axis=columns), we can see that the columns have been renamed to X, Y, and Z.

Example 2: Rename DataFrame Index

Now, let’s rename the rows (index) of the DataFrame. Starting with our modified DataFrame from Example 1:

Python
new_index = ['row1', 'row2', 'row3']
df = df.set_axis(new_index, axis=0)
print(df)

Output:

      X  Y  Z
row1  1  4  7
row2  2  5  8
row3  3  6  9

By setting axis=0 (or you can also use axis=index), the rows have been renamed to row1, row2, and row3.

Example 3: Rename Both Index and Columns

We can rename both the index and the columns in one step by chaining set_axis() calls:

Python
df = pd.DataFrame(data)  # Resetting to the original DataFrame

df = df.set_axis(['row1', 'row2', 'row3'], axis=0).set_axis(['X', 'Y', 'Z'], axis=1)
print(df)

Output:

      X  Y  Z
row1  1  4  7
row2  2  5  8
row3  3  6  9

In this example, both the index and columns have been renamed.

Example 4: Using set_axis() with copy Parameter

If you prefer not to modify the original DataFrame and want to create a new one with the updated axis labels, use the copy parameter.

Python
df = pd.DataFrame(data)  # Resetting to the original DataFrame

new_columns = ['X', 'Y', 'Z']
new_index = ['row1', 'row2', 'row3']

# Using set_axis() with copy parameter
df_new = df.set_axis(new_columns, axis=1, copy=True).set_axis(new_index, axis=0, copy=True)
print(df_new)
print("\nOriginal DataFrame remains unchanged:")
print(df)

Output:

      X  Y  Z
row1  1  4  7
row2  2  5  8
row3  3  6  9

Original DataFrame remains unchanged:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

The original DataFrame remains unchanged, and a new DataFrame with the updated axis labels is returned. This demonstrates how the copy parameter allows for non-destructive modifications.

Conclusion

The set_axis() method in Pandas is a efficient way to rename the labels of a DataFrame’s axes. Whether you need to rename columns, rows, or both, this method provides a straightforward solution. By understanding and utilizing set_axis(), you can make your DataFrame more readable and better suited to your data analysis needs.

Also Explore:

Leave a Comment