• Post author:
  • Post category:Pandas
  • Post last modified:December 11, 2024
  • Reading time:12 mins read
You are currently viewing How to Convert Pandas Uppercase Column

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.

Advertisements

Key Points –

  • Ensure that the Pandas library is imported to access its functionalities.
  • Utilize string methods like str.upper() for direct conversion of string entries.
  • Apply the str.upper() method on the selected column to convert all text to uppercase.
  • The apply() function can be used to apply a function to each element in the column.
  • Lambda functions can provide a flexible way to customize the conversion process.
  • The map() function can also apply a transformation function across the column efficiently.
  • Alternatively, use the apply() method with a lambda function to convert the column to uppercase.

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)

FAQ on Convert Pandas Uppercase Column

How do I convert a single column to uppercase in a Pandas DataFrame?

To convert a single column to uppercase in a Pandas DataFrame, you can use the .str.upper() method.

Can I convert multiple columns to uppercase simultaneously?

You can convert multiple columns to uppercase simultaneously. Here’s how to do it using the .apply() method with .str.upper()

What happens if I apply .str.upper() to a column with non-string data?

The .str.upper() method will raise an error if applied to non-string data. Ensure the column contains string values or convert it first using .astype(str).

Can I convert all columns to uppercase at once?

Use .applymap() to apply .upper() across the entire DataFrame if all columns are strings.

How do I convert column names to uppercase instead of their values?

To convert the column names to uppercase in a Pandas DataFrame, you can use the .str.upper() method on the columns attribute.

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

References