Applying uppercase to a column in pandas dataframe

Given a pandas dataframe, we have to apply uppercase to a column. By Pranit Sharma Last updated : September 29, 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.

A string is a group of characters, these characters may consist of all the lower case, upper case, 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.

Applying uppercase to a column

Here, we are going to create a DataFrame with multiple columns and different data types. Out of these columns, the column which is of the object Data type will contain all the values in form of strings, we will convert all these strings using the upper() method which is a method of string.

We will access each value of this column and each time we will deal with the data in form of a string hence we will apply the upper() method to these values.

Let us understand with the help of an example,

Python program to apply uppercase to a column in pandas dataframe

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'Roll_No':[101,102,103,104,105],
    'Name':['Sharan Kumar','Prem Bharadwaj','Sunny Rathod','Pramod Mishra','Deepak Chandak'],
    'Stream':['PCM','PCM','PCB','COM','PCB']
}

# Creating DataFrame
df = pd.DataFrame(d)

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

# Applying upper method to each value 
# of name column
df['Name'] = df['Name'].str.upper()

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

Output

The output of the above program is:

Example: Applying uppercase to a column

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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