How to Convert Pandas Uppercase Column

  • Post author:
  • Post category:Pandas
  • Post last modified:November 20, 2023

You can convert column values to uppercase in pandas DataFrame by using 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 with examples.

1. Quick Examples of Convert Pandas Uppercase Column

If you are in a hurry, below are some quick examples of how to convert column values to uppercase in pandas DataFrame.


# 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)

Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are CoursesFeeDuration and Discount.


# 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

2. Convert Pandas Column to Uppercase using str.upper()

We can use str.upper() function to convert DataFrame column values to uppercase. For that, we 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)

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

3. 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)

Yields the same output as above.

4. 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)

Yields the same output as above.

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)

Yields the same output as above.

5. Use map() Function to Convert Column to Uppercase


# 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)

7. Conclusion

In this article, I have explained how to convert specified column values of a given Pandas DataFrame from lowercase to uppercase by using str.upper(), map(), apply(), and lambda functions with examples.

Happy Learning !!

Related Articles

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing How to Convert Pandas Uppercase Column