To convert a Pandas DataFrame column to lowercase, you can use the str.lower()
, map()
, apply()
, and lambda
function. This method allows you to apply a string function directly to each element of the column.
In this article, I will explain how to convert the entire contents of a column from uppercase to lowercase in a Pandas DataFrame with examples.
Key Points –
- Pandas provides the
str.lower()
method to convert string values to lowercase within a DataFrame column. - Apply the
str.lower()
method to the desired column directly, or via theapply()
function. - Ensure the column data type is compatible with string operations before applying
str.lower()
. - The
str.lower()
method works efficiently with both string and object data types in Pandas DataFrame columns.
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.
# Quick examples of convert lowercase column values
# 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)
To run some examples of converting pandas column to lowercase, let’s create Pandas DataFrame using data from a dictionary.
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("Create DataFrame:\n", df)
Yields below output.
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 to 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("Convert Pandas column to lower case:\n", df)
Yields below output.
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 and 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("Convert Pandas column to lower case:\n", df)
Yields the same output as above.
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("Convert Pandas column to lower case:\n", df)
Yields the same output as above.
Use map() Function
we can use the map() function to convert column values of a given DataFrame from uppercase to lowercase. For that, we need to pass str.lower() function into the map() function and 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("Convert Pandas column to lower case:\n", df)
Yields the same output as above.
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)
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
- Convert Pandas Index to List
- Pandas Convert String to Integer
- How to Use NOT IN Filter in Pandas
- Pandas DataFrame reindex() Function
- How to Convert List to Pandas Series
- Pandas Series.replace() – Replace Values
- Convert NumPy Array to Pandas DataFrame
- Convert Pandas Series of Lists to One Series
- Pandas – Convert DataFrame to JSON String
- Pandas – Convert DataFrame to Dictionary (Dict)