Renaming columns in a pandas DataFrame

In this section, we'll learn about various methods for renaming column labels in pandas. We will learn how to rename columns both after reading in data and while reading in data, and we'll see how to rename either all or specific columns. We will start by importing the pandas module into our Jupyter notebook:

import pandas as pd

There are a few ways by which we can rename the columns in a pandas DataFrame. One of the ways is to rename the columns while reading in the data from the dataset. To do this, we need to pass the column names as a list to the names parameter of the read_csv method:

list_columns= ['Date', 'Region ID', Region Name', 'State', 'City', 'County', 'Size Rank', 'Price']data = pd.read_csv('data-zillow.csv', ...

Get Mastering Exploratory Analysis with pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.