upper() function in pandas python – Convert the column to uppercase

In this section we will be using upper() function in pandas, to  convert the character column of the python pandas dataframe to uppercase. We have listed some of different ways to convert string column to upper case in pandas

  • If the input string is in any case (upper, lower or title) , upper() function in pandas converts the string to upper case.
  • use str.upper() function to convert pandas column to uppercase
  • use apply() function to convert pandas column to uppercase
  • use apply() and lambda function to convert pandas string column to uppercase
  • use map() function to convert pandas string column to uppercase
  • Capitalize first character of each word in pandas column – using title() function
  • Capitalize only first character of the entire sentence in pandas column – using capitalize() function

Lets look at these each case with an Example

 

Create dataframe:

## create dataframe

import pandas as pd 
d = {'Quarters' : ['quarter1','quarter2','quarter3','quarter4'], 'Description' : ['First Quarter of the year', 'second quarter of the year', 'Third Quarter of the year', 'FOURTH QUARTER OF THE YEAR']}
df=pd.DataFrame(d) 
df	

resultant dataframe will be

convert pandas column to uppercase - upper() function in pandas 11

 

 

Convert pandas column to uppercase : str.upper() function:

str.upper() function first converts a column into string column and then into upper case as shown below

 
### str.upper() function

df['Description_Upper'] = df['Description'].str.upper()
df

convert pandas column to uppercase - upper() function in pandas 12

 

 

Convert pandas column to uppercase : apply() function:

within apply() function string function is used and it converts the pandas column to upper case as shown below

 
### apply() function

df['Description_Upper'] = df['Description'].apply(str.upper)
df

convert pandas column to uppercase - upper() function in pandas 12

 

 

Convert pandas column to uppercase : apply() and lambda function:

within apply() function lambda function and string function is used to convert a column to upper case in pandas dataframe as shown below

 
### apply() function & lambda function

df['Description_Upper'] = df['Description'].apply(lambda x: x.upper())
df

convert pandas column to uppercase - upper() function in pandas 12

 

 

 

Convert pandas column to uppercase : using map() function:

within map() function str.upper() function is used to convert a column to upper case in pandas dataframe as shown below

 
### map() function

df['Description_Upper'] = df['Description'].map(str.upper)
df

convert pandas column to uppercase - upper() function in pandas 12

 

 

Convert pandas column to Title case : Capitalize first character of each word of pandas column

we can use str.title() to capitalize the first character of each word in the column, i.e. to convert a column to title case in pandas dataframe as shown below

# Title function in python to convert the character column to Title case 

df['Description_Title'] = df['Description'].str.title() 
df

so the output will be

convert pandas column to Title case title() function in pandas 1 Captialize first letter of every word in pandas

 

 

Convert pandas column to Caplitalize case : Capitalize first character of entire string of the pandas column

we can use str.capitalize() to capitalize the only first character of entire sentence in the column, i.e. to convert a column to capitalize case in pandas dataframe as shown below

# capitalize function in python to convert the character column to capitalize case 

df['Description_Capitalize'] = df['Description'].str.capitalize()
df

so the output will have only first character of entire sentence is capitalized
convert pandas column to Title case title() function in pandas 1 Captialize first letter of entire string
 

 

 

 

previous Convert the character column of the python Dataframe to uppercase                                                                                                           next Convert the character column of the python Dataframe to uppercase

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.