Capitalize first letter of column name in pandas python

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

  • Capitalize first character or first letter of column name in pandas python using str.capitalize () function
  • Capitalize first letter of all column names in pandas python using map() & lower() functions
  • Capitalize first character of all column names in pandas python using list comprehension – i.e. we will be passing all the column names as list to capitalize() 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

The Resultant dataframe is

 

pandas-capitalize-first-letter-in-column-name-1

 

 

Capitalize first character or first letter of column name in pandas python str.capitalize(): Method 1

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

 

## Method 1: capitalize first character of Column Names using str.lower()

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

The Resultant dataframe with all the column names with first letter converted to upper case is

 

pandas-capitalize-first-letter-in-column-name-2

 

 

 

Capitalize first letter of all column names in pandas python using map() & lower() functions: Method 2

In the second method, we will be using  str.capitalize() function  along with the map() function . In the map() function str.capitalize and columns names are passed as the argument as shown below, which in turn will Capitalize first letter of all column names

 
## Method 2: Convert Dataframe Column name's first letter to capital letter using map() & lower()

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

The Resultant dataframe with all the column names with first letter converted to upper case is

pandas-capitalize-first-letter-in-column-name-2

 

 

 

Capitalize first character of all column names in pandas python using list comprehension: Method 3

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

 
## Method 3: column name's first letter to capital letter using List Comprehension

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

The Resultant dataframe with all the column names with first letter converted to upper case is

pandas-capitalize-first-letter-in-column-name-2

 

 

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