Links

Rename Columns

Tags: #pandas #snippet #datacleaning #operations
Author: Florent Ravenel​
Description: This notebook renames columns in a dataframe.

Input

Import libraries

import pandas as pd

Setup DataFrame

# create DataFrame
df = pd.DataFrame(
{
"team": ["A", "A", "A", "B", "B", "B"],
"points": [11, 7, 8, 10, 21, 13],
"assists": [5, 7, 7, 9, 12, 0],
"rebounds": [5, 8, 10, 6, 6, 22],
}
)
df

Define columns to rename

This function will not throw an error if column does not exist in dataframe.
# dict of columns with new names
to_rename = {
"team": "equipo",
"points": "puntos",
"assists": "asistancias",
"rebounds": "rebotes",
"col": "new_col",
}

Model

Rename columns in dataframe

df = df.rename(columns=to_rename)

Output

View new DataFrame

df