Convert Pandas Column to Lowercase

Spread the love

You can convert column values to lowercase in pandas DataFrame by using str.lower(), map(), apply() and lambda function. In this article, I will explain how to convert uppercase column values into lowercase column values of pandas DataFrame with examples.

1. Quick Examples of Convert Lowercase Column Values

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


# Below are the quick examples

# Example 1: convert lowercase column using str.lower()
df['Courses'] = df['Courses'].str.lower()

# Example 2: convert lowercase column using apply()
df['Courses'] = df['Courses'].apply(str.lower)

# Example 3: Use apply() & lambda function
df['Courses'].apply(lambda x: x.lower())

# Example 4: Convert pandas column to lowercase use map()
df['Courses'] = df['Courses'].map(str.lower)

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. Pandas Convert Column to Lowercase using str.lower()

We can use str.lower() function to convert Pandas DataFrame column values to lowercase. This is a function from Series hence you can easily apply this function on a specific column. This syntax will convert specified column values from uppercase to lowercase. Here we convert the column values and assign it back to the same column.


# convert lowercase column use str.lower()
df['Courses'] = df['Courses'].str.lower()
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. Convert Pandas Column to Lowercase using apply()

Similarly, we can use apply() function to convert column values of a given DataFrame to lowercase. For that, we need to pass str.lower() function into apply() function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].apply(str.lower) this syntax converts uppercase column values to lowercase column values.


# convert lowercase column use apply()
df['Courses'] = df['Courses'].apply(str.lower)
print(df)

Yields the same output as above.

4. Use apply() & Lambda Function

Alternatively, we can pass the lambda function into apply() function we can convert specified column values of a given DataFrame from uppercase to lowercase. Here, the lambda expression is used to construct an anonymous function.


# Use apply() & lambda function
df['Courses'].apply(lambda x: x.lower())
print(df)

Yields the same output as above.

5. Use map() Function

we can use map() function to convert column values of a given DataFrame from uppercase to lowercase. For that, we need to pass str.lower() function into map() function then, call the specified column of the given DataFrame. df['Courses']=df['Courses'].map(str.lower) this syntax converts uppercase column values to lowercase column values.


# Convert pandas column to lowercase use map()
df['Courses'] = df['Courses'].map(str.lower)
print(df)

Yields the same output as above.

6. Complete Example For Lowercase Column Values


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)

# convert lowercase column use str.lower()
df['Courses'] = df['Courses'].str.lower()
print(df)

# convert lowercase column use apply()
df['Courses'] = df['Courses'].apply(str.lower)
print(df)

# Use apply() & lambda function
df['Courses'].apply(lambda x: x.lower())
print(df)

# Convert pandas column to lowercase use map()
df['Courses'] = df['Courses'].map(str.lower)
print(df)

7. Conclusion

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

Happy Learning !!

Related Articles

References

Leave a Reply

You are currently viewing Convert Pandas Column to Lowercase