How can I make pandas dataframe column headers all lowercase?

PythonPandasDataframe

Python Problem Overview


I want to make all column headers in my pandas data frame lower case

Example

If I have:

data =

  country country isocode  year     XRAT          tcgdp
0  Canada             CAN  2001  1.54876   924909.44207
1  Canada             CAN  2002  1.56932   957299.91586
2  Canada             CAN  2003  1.40105  1016902.00180
....

I would like to change XRAT to xrat by doing something like:

data.headers.lowercase()

So that I get:

  country country isocode  year     xrat          tcgdp
0  Canada             CAN  2001  1.54876   924909.44207
1  Canada             CAN  2002  1.56932   957299.91586
2  Canada             CAN  2003  1.40105  1016902.00180
3  Canada             CAN  2004  1.30102  1096000.35500
....

I will not know the names of each column header ahead of time.

Python Solutions


Solution 1 - Python

You can do it like this:

data.columns = map(str.lower, data.columns)

or

data.columns = [x.lower() for x in data.columns]

example:

>>> data = pd.DataFrame({'A':range(3), 'B':range(3,0,-1), 'C':list('abc')})
>>> data
   A  B  C
0  0  3  a
1  1  2  b
2  2  1  c
>>> data.columns = map(str.lower, data.columns)
>>> data
   a  b  c
0  0  3  a
1  1  2  b
2  2  1  c

Solution 2 - Python

You could do it easily with str.lower for columns:

df.columns = df.columns.str.lower()

Example:

In [63]: df
Out[63]: 
  country country isocode  year     XRAT         tcgdp
0  Canada             CAN  2001  1.54876  9.249094e+05
1  Canada             CAN  2002  1.56932  9.572999e+05
2  Canada             CAN  2003  1.40105  1.016902e+06

In [64]: df.columns = df.columns.str.lower()

In [65]: df
Out[65]: 
  country country isocode  year     xrat         tcgdp
0  Canada             CAN  2001  1.54876  9.249094e+05
1  Canada             CAN  2002  1.56932  9.572999e+05
2  Canada             CAN  2003  1.40105  1.016902e+06

Solution 3 - Python

If you want to do the rename using a chained method call, you can use

data.rename(columns=str.lower)

Solution 4 - Python

df.columns = df.columns.str.lower()

is the easiest but will give an error if some headers are numeric

if you have numeric headers then use this:

df.columns = [str(x).lower() for x in df.columns]

Solution 5 - Python

I noticed some of the other answers will fail if a column name is made of digits (e.g. "123"). Try these to handle such cases too.

Option 1: Use df.rename

def rename_col(old_name):
    return str(old_name).lower()

df.rename(rename_col)

Option 2 (from this comment):

df.columns.astype(str).str.lower()

Solution 6 - Python

Another convention based on the official documentation:

frame.rename(mapper=lambda x:x.lower(), axis='columns', inplace=True)

> Parameters: mapper: Dict-like or function transformations to apply to that axis’ values. Use either mapper and axis to specify the axis to target with mapper, or index and columns.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnatsuki_2002View Question on Stackoverflow
Solution 1 - PythonRoman PekarView Answer on Stackoverflow
Solution 2 - PythonAnton ProtopopovView Answer on Stackoverflow
Solution 3 - PythontheisterView Answer on Stackoverflow
Solution 4 - PythonChadee FouadView Answer on Stackoverflow
Solution 5 - PythonPrincyView Answer on Stackoverflow
Solution 6 - Pythonkc12View Answer on Stackoverflow