logo

Renaming Columns

Column names from files are often messy - spaces, inconsistent casing, abbreviations. Use rename() to fix them.

Rename specific columns with a dictionary:

df = df.rename(columns={'old_name': 'new_name', 'ID': 'user_id'})

Apply a function to all column names:

df.columns = df.columns.str.lower()
df.columns = df.columns.str.replace(' ', '_')

Chain transformations:

df.columns = df.columns.str.lower().str.replace(' ', '_')

Clean column names make your code more readable and prevent errors from typos.

For data cleaning best practices, see The Ultimate Pandas Bootcamp.