Convert entire dataframe to lower case [SOLVED]

In this article we will see how we can lower case entire dataframe in pandas,  python is usually used when we are working with huge datasets and data handling is an important task before we train any dataset.

 

Pandas in Python

Pandas is an open source package that is widely used for data analysis and data manipulation. To train large datasets in artificial intelligence or machine learning. We will use pandas to lower case entire dataframe. To install pandas in your system, you need to type in the following command in your terminal

Advertisement
pip install pandas

 

Lower Case Entire Dataframe

Lets say we have a dataset and we want to convert it into lowercase, there are some techniques for doing so.

 

Method-1: Use applymap method

This method, returns a scalar to every element of the dataset. It is a method of the pandas.Dataframe package. So in our case if we pass it the dataframe and the lowercase argument to it the we can  lower case entire dataframe. We also pass the dictionary that is the data frame as an argument.

# Create an example dataframe about a fictional army

import pandas as pd

# lower case entire dataframe
raw_data = {'age': ['12', '14', '15', '18'],
            'color': ['black', 'Yellow', 'RED', 'BLUE'],
            'height': ['5 2', '5 3', '2 5', '4 5'],
            }

df = pd.DataFrame(raw_data, columns = ['age', 'color', 'height'])
print("before lower case entire dataframe\n",df)

df = df.applymap(lambda s: s.lower() if type(s) == str else s)
print("after lower case entire dataframe\n",df)

The output of this code is

before lower case entire dataframe   
age   color height
0  12   black    5 2
1  14  Yellow    5 3
2  15     RED    2 5
3  18    BLUE    4 5

after lower case entire dataframe   
age   color height
0  12   black    5 2
1  14  yellow    5 3
2  15     red    2 5
3  18    blue    4 5

 

Method-2: Create a lambda function

Lambda functions are anonymous functions that can take any argument and returns an expression. In python they work fast so we can use it here

The code becomes

# Create an example dataframe about a fictional army
import pandas as pd

# lower case entire dataframe
raw_data = {'age': ['12', '14', '15', '18'],
            'color': ['black', 'Yellow', 'RED', 'BLUE'],
            'height': ['5 2', '5 3', '2 5', '4 5'],
            } 

df = pd.DataFrame(raw_data, columns = ['age', 'color', 'height'])
print("print data before lower case entire dataframe\n" ,df)

df = df.apply(lambda x: x.str.lower() if x.dtype == "object" else x) 
print("print data after lower case entire dataframe\n" ,df)

The output of this code is :

Advertisement
print data before lower case entire dataframe   
age   color height
0  12   black    5 2
1  14  Yellow    5 3
2  15     RED    2 5
3  18    BLUE    4 5

print data after lower case entire dataframe   
age   color height
0  12   black    5 2
1  14  yellow    5 3
2  15     red    2 5
3  18    blue    4 5

 

Conclusion

In this article we studied different techniques to lower case entire dataframe in python pandas. We studied a bit about pandas and how they work for data manipulation and then found the solution to lowercase a dataframe using different approaches. First was to use the applymap method and pass the lower string argument to it so that each entry is changed accordingly and secondly we can use a lambda function that returns the lowercase of every entry.

 

Further Reading

Convert whole dataframe from lower case to upper case with Pandas
Pandas Oracle

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X