How to use the upper() function in pandas
Overview
The upper() function of the pandas library in Python converts all characters in a string to uppercase.
Syntax
upper()
Note: The
upper()function can’t be used on the entire column of a DataFrame.
Code example
The following code demonstrates how to use the upper() function in pandas:
import pandas as pd# Create a DataFramedf = pd.DataFrame({"name": ["Maria","Paul","Eva","Oliver","Sebastian","Camila"],"age":[20,22,32,26,29,30],"salary":[2000,2500,4000,3500,4200,3000],"gender":["female","male","female","male","male","female"]})# Convert string to uppercase using upper()print(df["name"][1].upper())
Code explanation
-
Line 1: We import the
pandaslibrary. -
Lines 4–10: We create a DataFrame,
df, from a dictionary. -
Line 13: We use the
upper()function to convertnameat index1to uppercase. Then, we print the output.
Note: If we use the
upper()method on the entire column, we will get an error.