Converting column names to lowercase using str.lower() in Pandas
Pandas: Data Cleaning and Preprocessing Exercise-11 with Solution
Write a Pandas program to change column names to lowercase.
In this exercise, we have changed the column names of a DataFrame to lowercase using str.lower().
Sample Solution :
Code :
import pandas as pd
# Create a sample DataFrame with uppercase column names
df = pd.DataFrame({
'Name': ['Selena', 'Annabel', 'Caeso'],
'Age': [25, 30, 22],
'Salary': [50000, 60000, 70000]
})
# Convert all column names to lowercase
df.columns = df.columns.str.lower()
# Output the result
print(df)
Output:
name age salary 0 Selena 25 50000 1 Annabel 30 60000 2 Caeso 22 70000
Explanation:
- Created a DataFrame with uppercase column names.
- Used str.lower() to convert all column names to lowercase.
- Outputted the DataFrame with the new lowercase column names.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/pandas/pandas-convert-column-names-to-lowercase.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics