Change column names to lower case in pandas python

In This Section we will be focusing on how to change all column names to lower case in pandas python. Changing column names to lower case can be accomplished in multiple ways, we have used functions like str.lower() , map() along with list comprehension . let’s look at each of these cases in pandas with an example for each

  • Convert dataframe column names to lower case in pandas python using string.lower() function
  • Convert column names to lower case in pandas using map() & lower() functions
  • Change all the column names to lower case in pandas  python using list comprehension – i.e. we will be passing all the column names as list to lower() function

 

Create Dataframe:

 
## create dataframe 

import pandas as pd 

data = {'product_Name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
        'price': [1200, 150, 300, 450, 200],
        'location_name':['Mumbai','Delhi','California','Newyork','London'],
        'offer_percentage':[3,4,8,1,3],
        'Name_of_customer':['Krishna','Ram','Johnathan','Joel','Christy'],
        'name_of_country':['India','India','US','US','UK']
        }

df = pd.DataFrame(data)

df

 

Change-column-names-to-lower-case-in-pandas-python-1

 

 

 

Convert dataframe column names to lower case in pandas python using string.lower(): Method 1

As we have already drawn a conclusion that there are multiple methods to convert the column to lower case in pandas,  we will be discussing about three methods, In the first method, we will be applying str.lower() function over the column names of the dataframe which in turn will convert all the columns to lower case.

 

## Method 1: Convert Dataframe Column Names to Lowercase using str.lower()

df.columns=df.columns.str.lower()
df

The Resultant dataframe with all the column names converted to lower case is

Change-column-names-to-lower-case-in-pandas-python-2

 

 

 

 

Convert column names to lower case in pandas using map() & lower() functions: Method 2

In the second method, we will be using  str.lower() function  along with the map() function . In the map() function str.lower and columns names are passed as the argument as shown below, which in turn will convert all the columns to lower case.

 
## Method 2: Convert Dataframe Column Names to lowercase using map(), lower()

df.columns=map(str.lower, df.columns)
df

The Resultant dataframe with all the column names converted to lower case is

Change-column-names-to-lower-case-in-pandas-python-2

 

 

 

Change all the column names to lower case in pandas using list comprehension: Method 3

In the third method, we will be passing converting all the column names as list and for each element of the list we use lower() function as shown below.

 
## Method 3: Convert Dataframe Column Names to lowercase using List Comprehension

df.columns= [column.lower() for column in df.columns]
df

The Resultant dataframe with all the column names changed to lower case is

Change-column-names-to-lower-case-in-pandas-python-3

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts