Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Change Column Names to Lowercase in Pandas DataFrame

  • 10 Jan, 2025
  • Com 0
Image of Changing Column Names to Lowercase in Pandas DataFrame

The fastest way to change the column names to lowercase in Pandas DataFrame is to use the df.columns.str.lower() function. It directly modifies the columns attribute of the DataFrame.

import pandas as pd

data = {'COL1': [1, 2, 3],
        'COL2': [4, 5, 6],
        'COL3': [7, 8, 9]}

df = pd.DataFrame(data)
print(df)

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

# After changing column names
print("\nAfter converting to lowercase:")
print(df)

Output

Change Column Names to Lowercase in Pandas DataFrame using df.columns.str.lower()

As illustrated in the above image, all column names are converted to lowercase.

This method is a one-liner and straightforward to understand. It is optimized for string operations and is ideal for converting to lowercase, uppercase, or removing spaces.

However, if you are looking at complex string operations, this approach is unsuitable. It does not support conditional transformation.

Using apply() with lambda

First, we will convert column names into Pandas Series objects and then execute the apply() function, which accepts a lambda function to convert each column name to lowercase.

import pandas as pd

data = {'COL1': [1, 2, 3],
        'COL2': [4, 5, 6],
        'COL3': [7, 8, 9]}

df = pd.DataFrame(data)
print(df)

# Change column names to lowercase using apply() and lambda
df.columns = df.columns.to_series().apply(lambda x: x.lower())

# After changing column names
print("\nAfter converting to lowercase:")
print(df)

The output looks like the image below visually:

Using apply() with lambda

Using this approach, we can chain multiple operations to get additional transformations. It provides flexibility to do anything with our columns, which gives us a robust edge over the first approach.

However, with great power comes great responsibility, and this approach is slower than df.columns.str.lower() method.

That’s all!

Post Views: 110
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Remove Spaces from Column Names in Pandas DataFrame
How to Get the Data Type of Column in Pandas DataFrame

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend