How to make pandas DataFrame column headers all lowercase?

Given a Pandas DataFrame, we have to make its column headers all lowercase.
Submitted by Pranit Sharma, on June 22, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consists of rows, columns, and the data.

Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. Each column has a specific header/name.

To convert all the column headers into lower case, we will use str.lower() method where str is nothing but each of the column header.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code to make pandas DataFrame column headers all lowercase

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Create a DataFrame
df = pd.DataFrame([
    [10,20,30,40],
    [50,60,70,80],
    [90,100,110,120]
    ],
    columns=['One','Two','Three','Four']
    )

# Display original DataFrame
print("Orignal DataFrame:\n",df,"\n")

# Converting all the header name into lower case
df.columns = [x.lower() for x in df.columns]

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output:

Example: Make pandas DataFrame column headers all lowercase

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.