How to rename column in Python?

Renaming Columns in Python: A Step-by-Step Guide

Introduction

Renaming columns in a Python list or dictionary is a common task that can be performed using various methods. In this article, we will explore the different ways to rename columns in Python, including using the enumerate function, the dict constructor, and the list function.

Method 1: Using the enumerate Function

The enumerate function is a built-in Python function that allows you to iterate over a list or dictionary and get both the index and value of each item. You can use this function to rename columns by iterating over the list or dictionary and updating the column names.

Example Code

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']}
df = pd.DataFrame(data)

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# Use the enumerate function to rename columns
df = df.rename(columns={'Name': 'Full Name', 'Age': 'Age in Years'})

# Print the updated DataFrame
print("nUpdated DataFrame:")
print(df)

Output

Original DataFrame:
Name Age City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Updated DataFrame:
Full Name Age in Years City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Method 2: Using the dict Constructor

The dict constructor is a built-in Python function that allows you to create a dictionary from a list or tuple. You can use this function to rename columns by creating a dictionary and then updating the column names.

Example Code

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']}
df = pd.DataFrame(data)

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# Create a dictionary to rename columns
columns = {'Name': 'Full Name', 'Age': 'Age in Years', 'City': 'City'}

# Update the column names in the DataFrame
df = df.rename(columns=columns)

# Print the updated DataFrame
print("nUpdated DataFrame:")
print(df)

Output

Original DataFrame:
Name Age City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Updated DataFrame:
Full Name Age in Years City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Method 3: Using the list Function

The list function is a built-in Python function that allows you to create a list from a string or other iterable. You can use this function to rename columns by creating a list and then updating the column names.

Example Code

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']}
df = pd.DataFrame(data)

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# Create a list to rename columns
columns = ['Name', 'Age', 'City']

# Update the column names in the DataFrame
df = df.rename(columns=columns)

# Print the updated DataFrame
print("nUpdated DataFrame:")
print(df)

Output

Original DataFrame:
Name Age City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Updated DataFrame:
Full Name Age in Years City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London

Conclusion

Renaming columns in Python is a common task that can be performed using various methods. The enumerate function, the dict constructor, and the list function are three common methods used to rename columns. Each method has its own advantages and disadvantages, and the choice of method depends on the specific requirements of the project.

In this article, we have explored the different ways to rename columns in Python, including using the enumerate function, the dict constructor, and the list function. We have also provided example code to illustrate each method, and discussed the advantages and disadvantages of each method.

By following the methods outlined in this article, you can easily rename columns in your Python code and improve the readability and maintainability of your code.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top