How to Convert Pandas Uppercase Column

Spread the love

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.


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


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

Leave a Reply

You are currently viewing How to Convert Pandas Uppercase Column