Renaming columns in a pandas DataFrame
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]:
In [5]:
# To check out only the columns
# It will output a list of columns
ufo.columns
Out[5]:
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]:
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]:
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]:
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]: