Last modified: Dec 08, 2024 By Alexander Williams
Python Pandas rename() Simplified
The rename()
function in Pandas is a versatile tool for renaming column or index labels in DataFrames. It’s essential for organizing data effectively.
What is rename()?
rename()
allows you to rename specific columns or index labels in a DataFrame or Series for better clarity and usability.
Basic Syntax of rename()
DataFrame.rename(mapper=None, *, index=None, columns=None, axis=None, copy=True, inplace=False)
Parameters:
index
: Dictionary-like or function to rename index labels.columns
: Dictionary-like or function to rename column labels.inplace
: Modify the DataFrame directly ifTrue
. Default isFalse
.
Renaming Columns in a DataFrame
Let’s start with renaming columns using a dictionary:
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
# Rename columns
renamed_df = df.rename(columns={'Name': 'Full Name', 'Age': 'Years'})
print(renamed_df)
Full Name Years
0 Alice 25
1 Bob 30
Renaming Index Labels
You can also rename index labels using the index
parameter:
# Rename index labels
renamed_index_df = df.rename(index={0: 'Row1', 1: 'Row2'})
print(renamed_index_df)
Name Age
Row1 Alice 25
Row2 Bob 30
Renaming Using Functions
Use a function to modify column or index labels:
# Use a function to rename columns
func_renamed_df = df.rename(columns=str.lower)
print(func_renamed_df)
name age
0 Alice 25
1 Bob 30
Using inplace=True
Modify the DataFrame in place without creating a new object:
# Rename columns in place
df.rename(columns={'Name': 'First Name'}, inplace=True)
print(df)
First Name Age
0 Alice 25
1 Bob 30
Applications of rename()
The rename()
method is particularly useful for:
- Standardizing column names for consistent analysis.
- Preparing datasets for merging or concatenation.
- Improving readability and clarity of data.
Learn about merging datasets with our guide on Pandas merge().
Renaming Rows and Columns Simultaneously
Rename both rows and columns in a single call:
# Rename both rows and columns
renamed_df = df.rename(index={0: 'Row1'}, columns={'Age': 'Years'})
print(renamed_df)
First Name Years
Row1 Alice 25
1 Bob 30
Conclusion
The rename()
function is a vital tool for data cleaning and organization in Pandas. It helps make datasets intuitive and easy to understand.
For advanced data handling, explore our guide on Pandas agg().