Convert whole dataframe from lowercase to uppercase with Pandas

Given a pandas dataframe, we have to convert whole dataframe fromlower case to uppercase. By Pranit Sharma Last updated : September 30, 2023

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.

Problem statement

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.

Converting whole dataframe from lowercase to uppercase

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 program 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

The output of the above program is:

Example: Convert whole dataframe from lowercase to uppercase

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.