In This Section we will be focusing on how to change all column names to upper case in pandas python. Changing column names to upper case can be accomplished in multiple ways, we have used functions like str.upper() , 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 upper case in pandas python using string.upper() function
- Convert column names to upper case in pandas using map() & upper() functions
- Change all the column names to upper case in pandas python using list comprehension – i.e. we will be passing all the column names as list to upper() 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
Convert dataframe column names to upper case in pandas python using string.upper(): Method 1
As we have already drawn a conclusion that there are multiple methods to convert the column to upper case in pandas, we will be discussing about three methods, In the first method, we will be applying str.upper() function over the column names of the dataframe which in turn will convert all the columns to upper case.
## Method 1: Convert Dataframe Column Names to Uppercase using str.upper() df.columns=df.columns.str.upper() df
The Resultant dataframe with all the column names converted to upper case is
Change or Convert column names to upper case in pandas using map() & upper() functions: Method 2
In the second method, we will be using str.upper() function along with the map() function . In the map() function str.upper and columns names are passed as the argument as shown below, which in turn will convert all the columns to upper case.
## Method 2: Convert Dataframe Column Names to Uppercase using map() & upper() df.columns=map(str.upper, df.columns) df
The Resultant dataframe with all the column names converted to upper case is
Change all the column names to upper 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 upper() function as shown below.
## Method 3: Convert Dataframe Column Names to uppercase using List Comprehension df.columns= [column.upper() for column in df.columns] df
The Resultant dataframe with all the column names changed to upper case is