Converting column names to lowercase using str.lower() in Pandas
11. Reordering and Splitting Columns
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.
For more Practice: Solve these Related Problems:
- Write a Pandas program to reorder columns in a DataFrame based on a specified custom order.
- Write a Pandas program to split a full name column into first, middle, and last name columns.
- Write a Pandas program to move a specific column to the beginning of the DataFrame and reorder the rest alphabetically.
- Write a Pandas program to split a column containing dates into separate day, month, and year columns.
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.