Renaming column headers in Pandas

When importing a file into a Pandas DataFrame, Pandas will use the first line of the file as the column names. If you have repeated names, Pandas will add .1 to the column name. Many times this is not ideal. The following recipe shows you how to rename the column headers in a Pandas DataFrame.

Getting ready

Create a Pandas DataFrame from a file of customer data:

import pandas as pd
import numpy as np
data_file = '../Data/customer_data.csv'
customers = pd.DataFrame.from_csv(data_file,
                       header=0,
                       sep=',',
                       index_col=0,
    encoding=None,
                       tupleize_cols=False)

How to do it…

customers.rename(columns={ 'birth date': 'date_of_birth', 'customer loyalty level': 'customer_loyalty_level', 'first name': 'first_name', 'last name': 'last_name', ...

Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.