Convert Entire Dataframe Columns to Lower case and Upper Case

Convert Entire Dataframe Columns to Lower case and Vice-versa

It can be time-consuming for you if some columns containing strings are of a different case. And it is more when the dataframe has a large dataset. Therefore it is best that you should convert all the strings or characters present in the pandas dataframe to lower or upper case. It makes it easy to traverse conditionally. In this entire tutorial, you will know the different methods to convert Convert Entire Dataframe Columns to Lower case or upper case.

Methods to Convert Entire Dataframe Columns to Lowercase

Let’s create a sample dataframe for the demonstration purpose. Here I will use the panads.Dataframe() method to convert the data to dataframe.

Execute the below lines of code.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df)

Output

Sample dataframe for converting dataframe columns to lower case
Sample dataframe for converting dataframe columns to lowercase

Method 1: Using apply() function

In the first method, I will use the pandas apply() method to convert the entire dataframe columns to lowercase. Here you also have to pass the lambda function that will take all the string values of the dataframe and convert them into lower cases.

Execute the below lines of code to achieve that.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
df2 = df.apply(lambda x: x.astype(str).str.lower())
print(df2)

Output

Lower case using apply() function
Lower case using apply() function

Method 2: Using the applymap() function

The second method to convert the dataframe string values to lowercase is the use of pandas applymap() function. Here you also have to pass the functions that will check the type of the values is a string or not. And if it is then convert it into lowercase.

Use the below lines of code to convert it.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
df2 = df.applymap(lambda s: s.lower() if type(s) == str else s)
print(df2)

Output

Lower case using applymap() function
Lower case using applymap() function

Methods to Convert Entire Dataframe Columns to Upper case

Just like you have converted the entire dataframe columns to lowercase in the above example you can also convert them to uppercase. In both method you have to use the str.upper() function instead of the str.lower().

Method 1: apply() function

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
df2 = df.apply(lambda x: x.astype(str).str.upper())
print(df2)

Output

Upper case using apply() function
Upper case using apply() function

Method 2:  applymap() function

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
df2 = df.applymap(lambda s: s.upper() if type(s) == str else s)
print(df2)

Output

Upper case using applymap() function
Upper case using applymap() function

Conclusion

I hope this tutorial has helped you to understand how to convert the pandas dataframe column’s strings to lower or upper case in pandas dataframe. There are different methods to achieve this task. I prefer using the ápply() function because it is easy to use and understand. If you have any questions, please feel free to ask in the comments section below. You can also contact us for more help.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner