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', names = list_columns)
data.head()

In the preceding example, we first created a list of column names we wanted; this should be the same number as the number of columns in the actual dataset. We then pass the list to the names parameter in the read_csv method. We then see that we have the column names we wanted, and so the read_csv method has changed the column names from what was there by default in the text file to the names we supplied.

We can also rename column names after the data has been read. Let's read the dataset in again from the CSV file, but this time without supplying any column names. We can rename columns by using the rename method. Let's first look at the columns from our dataset:

data.columns

Now, we call the rename method on the DataFrame and pass the column names, old and new values, to the columns parameter:

data.rename(columns={'RegionName':'Region', 'Metro':'City'}, inplace=True)

In the preceding code block, we change only a few column names rather than all of them. Let's call the columns property again to see if the column names have really been changed:

data.columns

Now, we have our new column names in the dataset.

We can also rename all columns after the data has been read, as follows:

data.columns = ['Date', 'Region ID', 'Region Name', 'State', 'City', 'County', 'Size Rank','Price']

We have set the columns property to a list of names to which we want all the columns to be renamed.

In this section, we learned about various methods for renaming column levels in pandas. We learned how to rename columns after reading in the data, and we learned how to rename columns while we are reading data from the CSV files. We also saw how to rename either all or specific columns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.41.27