How to lowercase Pandas DataFrame column names and values?

As part of your data preparation process you might need to drop one ore multiple redundant columns, calculate new column values or simply manipulate column names or values.

In this tutorial we’ll learn how to convert columns names and values to lowercase.

Creating example data

Let’s assume that we have this very simple example DataFrame. Feel free to use the code if you want to follow along with this example.

import pandas as pd

month = ['May', 'March', 'January', 'November', 'March']
language = ['Python', 'R', 'Python', 'Java', 'Javascript']
salary = [143.0, 143.0, 249.0, 168.0, 168.0]
salaries = dict(Month=month, Language=language, Monthly_Salary = salary)
s_df = pd.DataFrame(data=salaries)
s_df.head()

Here’s our data:

MonthLanguageMonthly_Salary
0MayPython143.0
1MarchR143.0
2JanuaryPython249.0
3NovemberJava168.0
4MarchJavascript168.0

Our column index is made of names written in UPPER case:

s_df.columns

This will return:

Index(['Month', 'Language', 'Monthly_Salary'], dtype='object')

Convert Pandas column to lowercase

We can convert columns in Pandas columns to lower characters. But first we need to covert the values to strings. We accomplish that using the str accessor and then applying the lower() function, which is available for strings.

s_df['Month'].str.lower()

This will render the month column values converted to lower characters:

0         may
1       march
2     january
3    november
4       march
Name: Month, dtype: object

Change all column headers to lowercase

Next let’s see how to convert all the column names:

cols = s_df.columns.str.lower()

This will render the following column index:

Index(['month', 'language', 'monthly_salary'], dtype='object')

Convert column names which contain specific text

We can easily convert part of our column headers to lowercase / uppercase. In the following example we filter the column index according to a specific string (using the contains() function) and then apply the lower() logic.

filt = s_df.columns.str.contains('guage')
s_df.columns[filt].str.lower()

Persisting your column name changes

As the inplace parameter is not readily available, we need to find a different way to persist our column name changes. Luckily, we have the DataFrame rename() method that allows to change the row or column index of our DataFrame according to a specific dictionary or function.

# define our function - in this case lower
l_case_func = str.lower

# rename the column axis index
s_df_lower =s_df.rename(columns = l_case_func)

Voila:

Index(['month', 'language', 'monthly_salary'], dtype='object')

Converting an entire DataFrame to lower case

In Pandas, the str accessor is available for indexes and Series objects. Trying to convert a DataFrame objects consisting of multiple columns to a string will return an Attribute error as shown below:

AttributeError: 'DataFrame' object has no attribute 'str'