Convert whole dataframe from lowercase to uppercase with Pandas

Given a pandas dataframe, we have to convert whole dataframe fromlower case to uppercase.
Submitted by Pranit Sharma, on September 12, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Converting whole dataframe from lowercase to uppercase

Suppose, we have a DataFrame with multiple columns. All these columns have string values and all the values are in lower case, we need to convert all these values from lower case to uppercase.

A string is a group of characters, these characters may consist of all the lowercase, uppercase, and special characters present on the keyboard of a computer system. A string is a data type and the number of characters in a string is known as the length of the string.

For this purpose, we will use apply() method so that we can access each column and for each column, we can access each string for which we can use the upper() method to convert the values from lowercase to uppercase.

Let us understand with the help of an example,

Python code to convert whole dataframe from lowercase to uppercase

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'str1':['a','b','c','d'],
    'str2':['e','f','g','h'],
    'str3':['i','j','k','l']
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display the DataFrame
print("Original DataFrame:\n",df,"\n")

# Converting the entire Dataframe from 
# lower to upper case
df = df.apply(lambda x: x.astype(str).str.upper())

# Display Modified DataFrame
print("Modified DataFrame:\n",df,"\n")

Output:

Example: Convert whole dataframe from lowercase to uppercase

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.