Open In App
Related Articles

Rename column by index in Pandas

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A column of a data frame can be changed using the position it is in known as its index. Just by the use of the index, a column can be renamed. Dealing with large and complex datasets in Pandas often requires manipulating column names for better analysis. Renaming columns by their index position can be an efficient and straightforward way to achieve this. This approach simplifies code, improves readability, and allows for easier manipulation of specific columns based on their position. This article discusses all such possible methods. 

What is the Column index?

A column index in a DataFrame serves two primary purposes:

  1. Labeling: It provides a unique identifier for each column in the DataFrame, similar to row labels for rows. This allows users to easily distinguish between columns and reference them explicitly.
  2. Location: It specifies the position of a column within the DataFrame. This allows accessing specific columns using their index position instead of relying on potentially ambiguous names.

Here’s a breakdown of the different types of column indices:

  • Integer index: This is the default indexing method in Pandas, where columns are automatically assigned sequential integer values starting from 0. This is simple and efficient for basic data manipulation.
  • Custom index: You can manually replace the default integer index with custom labels or other identifiers. This is useful when you want more descriptive or specific labels for your columns.
  • Multi-level index: In more complex data structures, you can use a multi-level index to hierarchically organize your columns. This allows grouping and filtering based on multiple criteria within the column structure.

Creating a Sample Dataframe

Here we are creating a Sample Dataframe:

Python3

import pandas as pd
 
# Creating a 4x4 sample DataFrame
data = {
    'id': [101, 102, 103, 104],
    'name': ['Ram', 'Ajay', 'Shweta', 'David'],
    'city': ['Patna', 'Uttar Pradesh', 'Delhi', 'Punjab'],
    'dob': ['1990-05-15', '1985-12-22', '1992-08-10', '1988-03-05']
}
 
index_labels = ['a', 'b', 'c', 'd']
 
df = pd.DataFrame(data, index=index_labels)
 
# Displaying the created DataFrame
print("Sample DataFrame:")
print(df)

                    

Output:

Sample DataFrame:
id name city dob
a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Renaming Columns by Index in Pandas

In this example, we are Switching and renaming specific columns in a pandas DataFrame by directly accessing and modifying their names using their column index positions.

Python3

# Renaming columns by index
df.columns.values[0] = "user_id"
df.columns.values[1] = "full_name"
df.columns.values[2] = "residence"
df.columns.values[3] = "birthdate"
 
# Displaying the DataFrame after renaming
print(df)

                    

Output:

user_id full_name      residence   birthdate
a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Pandas Rename Column and Index using Rename()

Pandas rename() method is used to rename any index, column, or row.

In this example, we will see how to rename a specific column (‘name’) in a pandas DataFrame to ‘full_name’ using its index. It assigns the index of the target column and the new name, then utilizes the rename() method with the specified column index and the new name.

Python3

# Renaming the 'name' column to 'full_name' by index
column_index_to_rename = 1  # Index of the 'name' column
new_column_name = 'full_name'
 
# Renaming the column using the 'rename' method
df.rename(columns={df.columns[column_index_to_rename]: new_column_name}, inplace=True)
 
# Displaying the DataFrame after renaming
print(df)

                    

Output:

 user_id full_name      residence   birthdate
a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Rename Column by Index using rename_col_by_index() Method

Pandas rename_col_by_index() method is used to rename any index, column, or row.

Syntax: def rename_col_by_index(dataframe, index_mapping)

Parameters:

  • dataframe: The input DataFrame.
  • index_mapping: A dictionary where keys are original column indices and values are new column names.

Returns: A dataframe with renamed columns.

In this example, we defines a function, rename_col_by_index, which takes a DataFrame and a dictionary mapping column indices to new names. The function renames columns based on the provided mapping using the enumerate function to iterate through columns. The code then applies this function to rename specific columns of a DataFrame (df).

Python3

def rename_col_by_index(dataframe, index_mapping):
    dataframe.columns = [index_mapping.get(i, col) for i, col in enumerate(dataframe.columns)]
    return dataframe
 
# Renaming columns using the function
new_column_mapping = {0: 'new_id', 2: 'new_city'}
df = rename_col_by_index(df, new_column_mapping)
 
# Displaying the DataFrame after renaming columns
print(df)

                    

Output:

 new_id    name       new_city         dob
a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Renaming column and index in Place

In this example, it demonstrates in-place renaming of a column (‘id’ to ‘new_id’) and an index (‘a’ to ‘row_a’) in a pandas DataFrame (df). The changes are made directly to the original DataFrame using the rename method with the inplace=True parameter.

Python3

# Renaming a column and an index in place
df.rename(columns={'id': 'new_id'}, index={'a': 'row_a'}, inplace=True)
 
# Displaying the DataFrame after renaming
print(df)

                    

Output:

 new_id    name       new_city         dob
row_a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Renaming two/more columns by Index in Pandas

In this example, we renaming of specific columns in a pandas DataFrame (df) using their index numbers. It assigns new names (‘new_id’ and ‘new_city’) to columns at index positions 0 and 2, respectively. After renaming, the code prints the DataFrame to show the updated column names.

Python3

# Renaming columns using index numbers
df.columns.values[[0, 2]] = ['new_id', 'new_city']
 
# Displaying the DataFrame after renaming columns
print(df)

                    

Output:

new_id full_name       new_city         dob
a 101 Ram Patna 1990-05-15
b 102 Ajay Uttar Pradesh 1985-12-22
c 103 Shweta Delhi 1992-08-10
d 104 David Punjab 1988-03-05

Rename Multiple Column/Index with Pandas

In this it creates a dictionary specifying changes for column and index names in a pandas DataFrame (df). The dictionary column_changes maps existing column names to new names, and index_changes maps existing index labels to new labels. The rename method is then used to apply these changes in place, directly modifying the original DataFrame.

Python3

# Creating a dictionary for column and index name changes
column_changes = {'id': 'new_id', 'name': 'full_name', 'dob': 'Birthdate'}
index_changes = {'a': 'row_a', 'b': 'row_b', 'c': 'row_c', 'd': 'row_d'}
 
# Changing multiple column and index names
df.rename(columns=column_changes, index=index_changes, inplace=True)
 
# Displaying the DataFrame after renaming
print(df)

                    

Output:

 new_id full_name       new_city   BirthDate
row_a 101 Ram Patna 1990-05-15
row_b 102 Ajay Uttar Pradesh 1985-12-22
row_c 103 Shweta Delhi 1992-08-10
row_d 104 David Punjab 1988-03-05

Conclusion

Understanding how to rename columns by index in Pandas empowers you to effectively manage your data and enhance your analysis workflow. This technique offers flexibility, code conciseness, and improved readability, making it a valuable tool for data manipulation within the Pandas framework.



Last Updated : 21 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads