Renaming columns in a pandas DataFrame
Renaming Columns

This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.

Renaming columns in a pandas DataFrame

In [1]:
import pandas as pd
In [2]:
url = 'http://bit.ly/uforeports'
ufo = pd.read_csv(url)
In [3]:
ufo.head()
Out[3]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [5]:
# To check out only the columns
# It will output a list of columns
ufo.columns
Out[5]:
Index(['City', 'Colors Reported', 'Shape Reported', 'State', 'Time'], dtype='object')

Method 1: Renaming a single column

In [8]:
# inplace=True to affect DataFrame
ufo.rename(columns = {'Colors Reported': 'Colors_Reported', 'Shape Reported': 'Shape_Reported'}, inplace=True)
In [9]:
ufo.columns
Out[9]:
Index(['City', 'Colors_Reported', 'Shape_Reported', 'State', 'Time'], dtype='object')

Method 2: Renaming multiple columns

In [10]:
ufo_cols = ['city', 'colors reported', 'shape reported', 'state', 'time']
In [11]:
ufo.columns = ufo_cols
In [13]:
ufo.head()
Out[13]:
city colors reported shape reported state time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00

Method 3: Change columns while reading

In [14]:
url = 'http://bit.ly/uforeports'
ufo = pd.read_csv(url, names=ufo_cols, header=0)
In [15]:
ufo.head()
Out[15]:
city colors reported shape reported state time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00

Method 4: Replacing spaces with underscores for all columns
If you have a 100 columns, some had spaces in them and you want to replace all the spaces with underscores

In [16]:
ufo.columns = ufo.columns.str.replace(' ', '_')
In [17]:
ufo.head()
Out[17]:
city colors_reported shape_reported state time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
Tags: pandas