Pandas Rename Column by Index


One of the most used Python libraries for data science and machine learning is Pandas. It offers a number of data exploration, cleaning, and transformation operations that are critical in working with data in Python.

Renaming columns in a Pandas DataFrame is a necessary task when working with datasets.

Pandas Library offers a straightforward way to achieve this. In this tutorial, we'll dive into how to rename columns using their index positions.

    Table of Contents

  1. Rename Columns by Index
  2. Rename Multiple Columns by Index
  3. Rename Columns Using a Loop
  4. Dynamically Rename Columns by Index
  5. Rename column using rename()
  6. Conclusion

1. Rename Columns by Index

Pandas provide df.columns attribute to get the column names of the DataFrame.

Lets say we want to rename the column 'A' to 'column_A'. We can do this by accessing the column name using its index position and assigning a new name to it.

Here is how we can do this:

import pandas as pd

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

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# ๐Ÿ‘‡ pandas rename column by index
df.columns.values[0] = 'column_A'

print("\nDataFrame after renaming first column:")
print(df)

Output:

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

DataFrame after renaming first column:
   column_A  B  C
0         1  4  7
1         2  5  8
2         3  6  9

As you can see, the column 'A' has been renamed to 'column_A'.


2. Rename Multiple Columns by Index

Suppose we want to rename the columns 'A' and 'B' to 'column_A' and 'column_B' respectively.

You can assign a list of new column names to the df.columns.values attribute and adjust the index positions accordingly.

import pandas as pd

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

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# ๐Ÿ‘‡ pandas rename multiple column by index
df.columns.values[0:2] = ['column_A', 'column_B']

print("\nDataFrame after renaming multiple columns:")
print(df)

Output:

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

DataFrame after renaming multiple columns:
   column_A  column_B  C
0         1         4  7
1         2         5  8
2         3         6  9

3. Rename Columns Using a Loop

You can also rename columns using a loop. This is useful when you have a large number of columns to rename.

First create a list of new column names and then use a for loop to iterate over the list and rename the columns.

import pandas as pd

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

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# ๐Ÿ‘‡ Renaming columns using a loop
new_column_names = ['column_A', 'column_B', 'column_C']
for i in range(len(df.columns)):
    df.columns.values[i] = new_column_names[i]

print("\nDataFrame after renaming columns:")
print(df)

Output:

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

DataFrame after renaming columns:
   column_A  column_B  column_C
0         1         4         7
1         2         5         8
2         3         6         9

4. Dynamically Rename Columns by Index

For a large DataFrame, it is not feasible to rename columns one by one. You can use a for loop to rename columns dynamically.

Suppose we want to rename the columns 'A', 'B', and 'C' to 'A_new', 'B_new', and 'C_new' respectively.

import pandas as pd

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

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# ๐Ÿ‘‡ Appending 'New' to all column names
df.columns = [f'{col}_new' for col in df.columns]

print("\nDataFrame after renaming columns:")
print(df)

Output:

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

DataFrame after renaming columns:
   A_new  B_new  C_new
0      1      4      7
1      2      5      8
2      3      6      9

5. Rename column using rename()

The rename() method is used to rename any index, column or row. It accepts a dictionary as input, where the keys are the old column names and the values are the corresponding new column names.

Syntax:

df.rename(columns = {'old_column_name':'new_column_name'}, inplace=True)

Here, inplace=True saves the changes right away in the df dataset.

import pandas as pd

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

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# ๐Ÿ‘‡ Using rename() method to rename columns
df.rename(columns={'A': 'Alpha', 'B': 'Beta', 'C': 'Charlie'}, inplace=True)

print("\nDataFrame after renaming columns:")
print(df)

Output:

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

DataFrame after renaming columns:
   Alpha  Beta  Charlie
0      1     4        7
1      2     5        8
2      3     6        9

Conclusion

In this section we covered renaming columns by index in Pandas, however, Pandas provides multiple approaches to renaming columns, catering to diverse needs and preferences.