Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to lowercase the column names in Pandas dataframe?
In this article, you'll learn how to convert column names and values to lowercase in a Pandas DataFrame. We'll explore three different methods: str.lower(), map(str.lower), and apply(lambda) functions.
Creating a Sample DataFrame
Let's start by creating a sample DataFrame to demonstrate the methods ?
import pandas as pd
# Create sample restaurant data
data = {
'Restaurant Name': ['Pizza Palace', 'Burger King', 'Sushi Bar'],
'Rating Color': ['Green', 'Yellow', 'Red'],
'Rating Text': ['Excellent', 'Good', 'Average']
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame: Restaurant Name Rating Color Rating Text 0 Pizza Palace Green Excellent 1 Burger King Yellow Good 2 Sushi Bar Red Average
Method 1: Using str.lower() Function
The str.lower() method converts string values in a column to lowercase ?
import pandas as pd
data = {
'Restaurant Name': ['Pizza Palace', 'Burger King', 'Sushi Bar'],
'Rating Color': ['Green', 'Yellow', 'Red'],
'Rating Text': ['Excellent', 'Good', 'Average']
}
df = pd.DataFrame(data)
# Convert Rating Color column to lowercase
print("Converting Rating Color to lowercase:")
df['Rating Color'] = df['Rating Color'].str.lower()
print(df['Rating Color'])
Converting Rating Color to lowercase: 0 green 1 yellow 2 red Name: Rating Color, dtype: object
Method 2: Using map(str.lower) Function
The map(str.lower) method applies the lowercase function to each element in the column ?
import pandas as pd
data = {
'Restaurant Name': ['Pizza Palace', 'Burger King', 'Sushi Bar'],
'Rating Text': ['Excellent', 'Good', 'Average']
}
df = pd.DataFrame(data)
# Add a new column with lowercase values
df['Lowercase Rating Text'] = df['Rating Text'].map(str.lower)
print("DataFrame with new lowercase column:")
print(df)
DataFrame with new lowercase column: Restaurant Name Rating Text Lowercase Rating Text 0 Pizza Palace Excellent excellent 1 Burger King Good good 2 Sushi Bar Average average
Method 3: Converting Column Names and Values
This method demonstrates converting both column values using apply(lambda) and column headers using map(str.lower) ?
import pandas as pd
data = {
'Restaurant Name': ['Pizza Palace', 'Burger King', 'Sushi Bar'],
'Rating Color': ['Green', 'Yellow', 'Red'],
'Rating Text': ['Excellent', 'Good', 'Average']
}
df = pd.DataFrame(data)
# Convert column values to lowercase using apply(lambda)
df['Lowercase Rating Text'] = df['Rating Text'].apply(lambda x: x.lower())
df['Lowercase Rating Color'] = df['Rating Color'].apply(lambda x: x.lower())
print("DataFrame with lowercase value columns:")
print(df)
# Convert column headers to lowercase
df.columns = df.columns.map(str.lower)
print("\nDataFrame with lowercase column headers:")
print(df)
DataFrame with lowercase value columns: Restaurant Name Rating Color Rating Text Lowercase Rating Text Lowercase Rating Color 0 Pizza Palace Green Excellent excellent green 1 Burger King Yellow Good good yellow 2 Sushi Bar Red Average average red DataFrame with lowercase column headers: restaurant name rating color rating text lowercase rating text lowercase rating color 0 Pizza Palace Green Excellent excellent green 1 Burger King Yellow Good good yellow 2 Sushi Bar Red Average average red
Comparison of Methods
| Method | Use Case | Syntax |
|---|---|---|
str.lower() |
Direct column conversion | df['col'].str.lower() |
map(str.lower) |
Creating new columns | df['col'].map(str.lower) |
apply(lambda) |
Custom transformations | df['col'].apply(lambda x: x.lower()) |
Conclusion
Use str.lower() for simple column conversion, map(str.lower) for creating new lowercase columns, and apply(lambda) for more complex transformations. To convert column headers, use df.columns.map(str.lower).
