change column names to lowercase of a pandas dataframe

Pandas – Change Column Names to Lowercase

In this tutorial, we will look at how to change the column names of a pandas dataframe to lowercase with the help of some examples.

How to convert column names to lowercase in a pandas dataframe?

change column names to lowercase of a pandas dataframe

You can use the pandas series .str.lower() method to rename all columns to lowercase in a pandas dataframe. Use the following steps –

  1. Access the column names using columns attribute of the dataframe.
  2. Change the column names to lower case using the .str.lower() method.
  3. Reset the column names of the dataframe to lowercase column names from above.

The following is the syntax –

# change all column names to lowercase
df.columns = df.columns.str.lower()

Examples

Let’s now look at some examples. First, we will create a dataframe that we will be using throughout this tutorial –

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

dataframe with employee data

Here, we created a dataframe with data of some employees in an office. You can see that the column names in the above dataframe are – “Name”, “Age”, and “Department”.

Example 1 – Convert all column names to lowercase

Let’s change all the column names to their respective lowercase values. We will use the syntax mentioned above using the .str.lower() function.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# change all column names to lowercase
df.columns = df.columns.str.lower()
# display the dataframe
df

Output:

employee dataframe with all columns in lowercase

You can see that now all the column names of the dataframe df are in lowercase.

Example 2 – Convert a specific column name to lowercase

If you want to change the name of a specific column to lowercase, you can use the pandas dataframe rename() function. Use the following syntax –

df = df.rename(columns={"OldName":"NewName"})

Let’s change the column name of the above dataframe to its original values. And then change only the column name “Department” to its lowercase value.

# reset column names
df.columns = ["Name", "Age", "Department"]
# change column name "Department" to lowercase
df = df.rename(columns={"Department":"department"})
# display the dataframe
df

Output:

employees dataframe with department column name in lowercase

You can see that the column name “Department” is now “department”.

Summary

In this tutorial, we looked at how to change the column names of a dataframe to lowercase. The following are the key takeaways –

  • Use .str.lower() method to change the column names (of all the columns) to lowercase.
  • If you want only want to change the column name (to lowercase or any other value) for a specific column (or columns), use the pandas dataframe rename() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top