To convert the values in a specific column of a pandas DataFrame to uppercase, you can use the str.upper()
, map()
, apply()
, and lambda
function. In this article, I will explain how to convert lowercase column values into uppercase column values of pandas DataFrame.
Quick Examples of Convert Pandas Uppercase Column
Following are quick examples of converting column values to uppercase in Pandas DataFrame.
# Quick examples of convert pandas uppercase column
# Example 1: Use str.upper() function
# to convert pandas column to uppercase
df['Courses'] = df['Courses'].str.upper()
# Example 2: Use apply() function
# To convert pandas column to uppercase
df['Courses'] = df['Courses'].apply(str.upper)
# Example 3: Use apply() & lambda function
df['Courses'].apply(lambda x: x.upper())
# Example 4: Use map() function
# To convert column to uppercase
df['Courses'] = df['Courses'].map(str.upper)
First, let’s create a Pandas DataFrame.
# convert pandas column to uppercase
import pandas as pd
import numpy as np
technologies= ({
'Courses':["spark","pyspark","hadoop","pandas"],
'Fee' :[22000,25000,24000,26000],
'Duration':['30days','50days','40days','60days'],
'Discount':[1000,2300,2500,1400]
})
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
0 spark 22000 30days 1000
1 pyspark 25000 50days 2300
2 hadoop 24000 40days 2500
3 pandas 26000 60days 1400
Using str.upper() to Convert Pandas Column to Uppercase
You can use str.upper()
method to convert DataFrame column values to uppercase
. For that, you will call str.upper()
function with a specified column of a given DataFrame. This syntax will convert specified column values from lowercase to uppercase.
# Use str.upper() function
# To convert pandas column to uppercase
df['Courses'] = df['Courses'].str.upper()
print(df)
# Output:
# Courses Fee Duration Discount
# 0 SPARK 22000 30days 1000
# 1 PYSPARK 25000 50days 2300
# 2 HADOOP 24000 40days 2500
# 3 PANDAS 26000 60days 1400
Use apply() Function to Convert Pandas Column to Uppercase
We can also use apply()
function to convert column values of a given DataFrame to uppercase. For that, we need to pass str.upper()
function into apply()
function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].apply(str.upper)
this syntax converts lowercase column values to uppercase column values.
# Use apply() function
# To convert pandas column to uppercase
df['Courses'] = df['Courses'].apply(str.upper)
print(df)
# Output:
# Courses Fee Duration Discount
# 0 SPARK 22000 30days 1000
# 1 PYSPARK 25000 50days 2300
# 2 HADOOP 24000 40days 2500
# 3 PANDAS 26000 60days 1400
Use apply() & Lambda Function
Alternatively, we can pass lambda
function into apply() function we can convert specified column values of a given DataFrame from lowercase to uppercase. Here, the lambda expression is used to construct an anonymous function.
# Use apply() & lambda function
df['Courses'].apply(lambda x: x.upper())
print(df)
# Output:
# Courses Fee Duration Discount
# 0 SPARK 22000 30days 1000
# 1 PYSPARK 25000 50days 2300
# 2 HADOOP 24000 40days 2500
# 3 PANDAS 26000 60days 1400
Use map() Function to Convert the Column to Uppercase
We can use map() function to convert column values of a given DataFrame from lowercase
to uppercase
. For that, we need to pass str.upper()
function into map()
function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].map(str.upper)
this syntax converts lowercase to uppercase column values.
# Use map() function
# To convert column to uppercase
df['Courses'] = df['Courses'].map(str.upper)
print(df)
# Output:
# Courses Fee Duration Discount
# 0 SPARK 22000 30days 1000
# 1 PYSPARK 25000 50days 2300
# 2 HADOOP 24000 40days 2500
# 3 PANDAS 26000 60days 1400
Complete Example For Convert Pandas Uppercase Column
# Use map() function
# To convert column to uppercase
import pandas as pd
import numpy as np
technologies= ({
'Courses':["spark","pyspark","hadoop","pandas"],
'Fee' :[22000,25000,24000,26000],
'Duration':['30days','50days','40days','60days'],
'Discount':[1000,2300,2500,1400]
})
df = pd.DataFrame(technologies)
print(df)
# Use str.upper() function
# To convert pandas column to uppercase
df['Courses'] = df['Courses'].str.upper()
print(df)
# Use apply() function
# To convert pandas column to uppercase
df['Courses'] = df['Courses'].apply(str.upper)
print(df)
# Use apply() & lambda function
df['Courses'].apply(lambda x: x.upper())
print(df)
# Use map() function
# To convert column to uppercase
df['Courses'] = df['Courses'].map(str.upper)
print(df)
Conclusion
In conclusion, this article has demonstrated various methods to convert the values of a specified column in a Pandas DataFrame from lowercase to uppercase. By using str.upper()
, map()
, apply()
, and lambda
methods, you can efficiently perform this transformation.
Happy Learning !!
Related Articles
- Pandas Convert JSON to DataFrame
- Pandas Handle Missing Data in Dataframe
- Pandas Convert Integer to Datetime Type
- Pandas Convert Datetime to Date Column
- Pandas Convert Date (datetime) to String Format
- Get First Column of DataFrame as Series
- Convert Row to Column Header in DataFrame
- Convert Pandas Column to Lowercase