How to Rename Columns in a Pandas DataFrame
In this short Pandas tutorial, you will learn how to rename columns in a Pandas DataFrame. Previously, you have learned how to append a column to a Pandas DataFrame but sometimes you also need to rename columns. Renaming columns is one of the, sometimes, essential data manipulation tasks you can carry out in Python.
Pandas DataFrame.Rename() Method:
If you need to rename columns in a Pandas DataFrame, you can use the following syntax:
df.rename(columns={'column name to change':'new column name'})
That is, the most important parameter in the rename() method, when you want to change name of a column, is the “columns” one. This takes a dictionary as argument. Now, you have to put the column names that you want to rename as key, and the new name as a value.
There are, however, other useful parameters that you can use when you need to rename columns in a Pandas dataframe. In the next sections, you will learn more about the rename() method.
How to Rename a Single Column in a Pandas DataFrame Example 1
In the first example, you are going to learn how to rename a single column in a DataFrame. Say that you have created the small, and simple, Pandas DataFrame from a Python dictionary:
import pandas as pd
df = pd.DataFrame({'A':range(0, 4),
'B':range(4, 8)})
Now, you realize that the column with the name “A” is not column A but column C! Well, now you just use the rename() method to change the name of the column:
df = df.rename(columns={'A':'C'})
Pretty simple, right! Now, in the next section you will learn how to use the inplace parameter when renaming a column in a DataFrame. After you have renamed your column you also need to change the type of a column, or two, in the Pandas dataframe.
How to Rename a Single Column in a Pandas DataFrame Example 2: Inplace
Now, in the second example, you are going to learn that it’s better to use the inplace parameter. You will, then, set it to True, when renaming a column in a Pandas DataFrame. Why? Well, I will show you why, you will understand why it’s better to use the inplace parameter.
df.rename(columns={'A':'C'},
inplace=True)
df
See, now you didn’t have to make a copy of the dataframe, as in the first example of how to rename a column in a Pandas DataFrame. Neat, right?
How to Rename Columns in a Pandas DataFrame Example 1
In the first, rename columns in a Pandas DataFrame you are going to use the columns method:
df.columns = ['AB', 'CD']
However, if you have a dataset with many columns this may not be the optimal way to do it. Thus, in the next section you will learn how to rename multiple columns in a Pandas DataFrame.
Rename Columns in a Pandas DataFrame Example 2
Now, in the third example, you are going to learn how to rename many columns in the Pandas DataFrame. First, however, you are going to load data from an URL.
import pandas as pd
csv_url = 'https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2018-financial-year-provisional/Download-data/annual-enterprise-survey-2018-financial-year-provisional-csv.csv'
df = pd.read_csv(csv_url)
df.head()
Now, as you can see, Pandas read_csv was used to read a CSV file from a URL. After that, you can use the rename() method, again, to change the column names:
df.rename(columns={'Industry_name_NZSIOC':'Industry_Name',
'Industry_aggregation_NZSIOC':'Industry_Aggregation',
'Industry_code_NZSIOC':'Industry_Code',
'Industry_code_ANZSIC06':'Industry_Code'}, inplace=True)
df.head()
Simlarily, you can read a CSV file in Python with the csv module. Make sure you check out that post! Finally, now when you have renamed your columns you may want to start exploring the data. Luckily, you can learn how to do barplots in Seaborn by many examples.
Summary
In this post, you learned how to rename columns in a Pandas DataFrame. This may, in fact, come in handy if you get data from a colleague. In this case, you may not have been part of naming the variables (columns). Thus, you may need to change the name of them in the Pandas DataFrame.