programmingdoor.com

How to rename Label and Column of DataFrame and Series in Pandas ?

BlogData Engineeringhow toPandasprogrammingpython

Data manipulation is a fundamental aspect of data analysis, and a significant part of this process is renaming columns in DataFrames and labels in Series. Python’s Pandas library provides several methods to accomplish this task. In this comprehensive guide, we’ll explore various techniques for renaming columns and labels in Pandas DataFrames and Series, giving you the flexibility and control you need to manage your data effectively.

Renaming Columns in Pandas DataFrames

DataFrames are central to working with tabular data. To rename columns, you can use the rename() method in Pandas. Here are some different ways to achieve this:

  1. Renaming with a Dictionary : One of the most common methods to rename columns is by providing a dictionary where keys are the current column names and values are the new names.
import pandas as pd

data = {'old_name1': [1, 2, 3], 'old_name2': [4, 5, 6]}
df = pd.DataFrame(data)

# Create a dictionary with new column names
new_names = {'old_name1': 'new_name1', 'old_name2': 'new_name2'}

# Rename the columns
df.rename(columns=new_names, inplace=True)

This code replaces ‘old_name1’ with ‘new_name1’ and ‘old_name2’ with ‘new_name2’.

  1. Renaming with a Function: You can also use a function to modify column names. For example, converting all column names to lowercase:
df.rename(columns=lambda x: x.lower(), inplace=True)

This will convert ‘new_name1’ and ‘new_name2’ to ‘new_name1’ and ‘new_name2’

  1. Renaming All Columns at Once : If you want to rename all columns simultaneously, you can directly assign a new list of column names to the DataFrame’s .columns attribute:
df.columns = ['new_col1', 'new_col2']

This will replace all existing column names with the new ones.

Renaming Labels in Pandas Series

In Pandas, Series is an essential data structure for handling one-dimensional data. You can rename labels in a Series with the rename() method as well. Here’s how:

  1. Renaming with a Dictionary

Just like with DataFrames, you can use a dictionary to rename labels in a Series. Let’s consider a Series example:

import pandas as pd

data = {'A': [1, 2, 3]}
s = pd.Series(data['A'])

# Create a dictionary with new labels
new_labels = {0: 'first', 1: 'second', 2: 'third'}

# Rename the Series
s.rename(index=new_labels, inplace=True)

Now, the Series will have labels ‘first,’ ‘second,’ and ‘third’ instead of 0, 1, and 2.

  1. Using the .rename_axis() Method

Pandas provides the .rename_axis() method to rename the index of a Series:

s.rename_axis('New Index Name', inplace=True)

This code changes the name of the Series’ index to ‘New Index Name.’