---
title: rename()
url: https://docs.vultr.com/python/third-party/pandas/DataFrame/rename
description: Rename Columns or Index
publish_date: 2024-12-31T16:40:00.068344Z
last_updated: 2025-04-10T16:14:28.571201Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1714/538c7cc0-00b5-4d76-a338-1a9a4557dd2c.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Pandas DataFrame rename() - Rename Columns or Index

## Introduction
The `rename()` method in the pandas library is a powerful and versatile tool for renaming axes labels and index names within DataFrame and Series objects. This method is particularly useful when you need to rename pandas DataFrame column or index names for clarity or to match specific formatting required for further data processing. It's a common task when cleaning and preparing data for analysis, making it essential for data scientists and analysts to understand and utilize it effectively.

In this article, you will learn how to use the `rename()` method to adaptively rename columns and indices in pandas DataFrames. Explore how to specify new names, use a mapping dictionary for bulk renaming, and perform in-place modifications to optimize data manipulation workflows.

## Renaming Columns - Python Pandas DataFrame

### Rename a Single Column
1. Import the pandas library and create a DataFrame.
1. Use the `rename()` method to rename a single column.

    ```python
    import pandas as pd

    df = pd.DataFrame({
        'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]
    })

    df = df.rename(columns={'A': 'Alpha'})
    print(df)
    ```

    In this example, the column named 'A' is renamed to 'Alpha'. The new DataFrame will reflect this change, displaying 'Alpha' as the header for the first column. This method effectively shows how to rename a column in pandas using `df.rename columns`.

### Rename Multiple Columns
1. Continue using the existing DataFrame.
1. Apply the `rename()` method with a dictionary that maps existing column names to new names in pandas DataFrame.

    ```python
    df = df.rename(columns={'B': 'Beta', 'C': 'Gamma'})
    print(df)
    ```

    The columns 'B' and 'C' are now renamed to 'Beta' and 'Gamma' respectively. Specify each old name and its corresponding new name in the dictionary passed to `rename()`. This approach is useful when working with pandas to rename multiple columns.

## Renaming Index - Python Pandas DataFrame

### Rename Index Using a Dictionary Map
1. Set an explicit index on a DataFrame.
1. Rename the index in pandas using a mapping dictionary.

    ```python
    df = pd.DataFrame({
        'Name': ['John', 'Jane', 'Alice'],
        'Score': [88, 92, 95]
    })
    df.index = ['a', 'b', 'c']

    df = df.rename(index={'a': 'first', 'b': 'second'})
    print(df)
    ```

    This snippet sets the index to ['a', 'b', 'c'] for the DataFrame and renames 'a' and 'b' to 'first' and 'second'. This can help in making indices more descriptive and meaningful.

### Rename Index Using a Function
1. Use a lambda function to apply a transformation to the indices.
1. Rename each index by appending a string or applying any function.

    ```python
    df = df.rename(index=lambda x: x.upper())
    print(df)
    ```

    Here, a lambda function is used to convert all index names to uppercase. The `rename()` method is flexible enough to accept any function that takes a single label and returns a modified label, providing an efficient way to rename the index column in pandas.

## In-Place Renaming

### Renaming without Creating a New DataFrame
1. Use the `inplace` parameter to modify the DataFrame in place.
1. Avoid assignments to a new DataFrame when not necessary.

    ```python
    df.rename(columns={'Name': 'Student_Name'}, inplace=True)
    print(df)
    ```

    Set `inplace=True` to apply changes directly to the original DataFrame without needing to assign the result to a new or the same DataFrame, thereby saving memory and processing time.

## Advanced Renaming Strategies

### Using Partial Functions for Conditional Renaming
1. Apply conditions within renaming functions to selectively rename DataFrame columns or indices in pandas.
1. Use Python's `functools.partial` to create reusable renaming functions.

    ```python
    from functools import partial

    def custom_rename(old_name, prefix):
        if old_name.startswith('S'):
            return prefix + old_name
        return old_name

    rename_with_prefix = partial(custom_rename, prefix='Grade_')
    df = df.rename(columns=rename_with_prefix)
    print(df)
    ```

    This technique is useful when renaming needs to be context-dependent or based on specific conditions, allowing users to rename columns in Python dynamically. Here, only column names starting with 'S' are prefixed with 'Grade_'.

## Conclusion
Pandas' `rename()` method offers a robust framework for renaming DataFrame columns and indices dynamically and efficiently. Whether making modifications for clarity, standardization, or advanced conditional renaming, this method provides both simplicity and power in data manipulation. Implement the techniques discussed to ensure your DataFrame columns and indices are appropriately labeled for any data analysis or processing task. By mastering `rename()`, you elevate your data handling capabilities to effectively prepare datasets for deeper analysis.