December 2015
Beginner to intermediate
202 pages
4h
English
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.
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)customers.rename(columns={ 'birth date': 'date_of_birth', 'customer loyalty level': 'customer_loyalty_level', 'first name': 'first_name', 'last name': 'last_name', ...Read now
Unlock full access