Pandas: How to rename columns

Summary: in this tutorial, you’ll learn about how to rename columns of a DataFrame.

String operation on column names

Column names of a DataFrame are basically strings wrapped in Index object. You can easily cast the df.columns to a Python list for later usage if needed.

Pandas support string operations directly on DataFrame columns.

In the example below, we uses str.lower() to convert column names to lowercase.

df = pd.DataFrame({'Month': [1, 4, 7, 10], 'Year': [2012, 2014, 2013, 2014], 'Sale': [55, 40, 84, 31]} ) df.columns = df.columns.str.lower() df
Code language: Python (python)
monthyearsale
01201255
14201440
27201384
310201431

Using the similar syntax, we can use .upper() to make the DataFrame column names uppercase.

df.columns = df.columns.str.upper()
Code language: Python (python)
MONTHYEARSALE
01201255
14201440
27201384
310201431

Python str.replace() can also be used to clean out unnecessary part of column names.

df = pd.DataFrame({'meta=Division': [1, 4, 7, 10], 'meta=Year': [2012, 2014, 2013, 2014], 'meta=SaleCount': [55, 40, 84, 31]} ) df.columns = df.columns.str.replace("meta=", "") df
Code language: Python (python)
DivisionYearSaleCount
01201255
14201440
27201384
310201431

Rename columns

There are several ways you can rename column names in Pandas. You can simply replace the existing column names by passing a new list.

df = pd.DataFrame({'meta=Division': [1, 4, 7, 10], 'meta=Year': [2012, 2014, 2013, 2014], 'meta=SaleCount': [55, 40, 84, 31]} ) df.columns = ['Code A', 'Code B', 'Code C'] df
Code language: Python (python)
Code ACode BCode C
01201255
14201440
27201384
310201431

Pandas also has a separate rename() function that can also be used to change multiple column names using a dict.

df = pd.DataFrame({'Division': [1, 4, 7, 10], 'Year': [2012, 2014, 2013, 2014], 'SaleCount': [55, 40, 84, 31]} ) col_replace = { 'Year': 'yr', 'SaleCount': 'sales', 'Division': 'branch', } df.rename(columns=col_replace,inplace=True) df
Code language: Python (python)

The original DataFrame column names will be replaced if it’s found in col_replace dictionary.

Leave a Comment