202

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.

Martin Thoma
139k174 gold badges688 silver badges1.1k bronze badges
asked Nov 1, 2013 at 11:39
2
  • 38
    More easiest df.columns = df.columns.str.lower() Commented Jul 12, 2018 at 21:06
  • 2
    Hello Nasuki, while the original answer was acceptable at the time of the original question, this answer below, is better. Please consider marking it as the accepted answer. Best Regards. Commented Aug 3, 2021 at 17:43

7 Answers 7

306

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
answered Nov 1, 2013 at 11:42
Sign up to request clarification or add additional context in comments.

7 Comments

Note that this can lead to duplicate column names when lower(column1) == lower(column2) (e.g., 'a' and 'A'). This can have unintended consequences when referencing columns later. (e.g. data['a'] will return a DataFrame, not a Series, with all columns named 'a'). See this gist for an example: gist.github.com/grisaitis/170e82a008480acb4fa3
[x.lower() for x in data.columns] is equivalent to: [x.lower() for x in data]
While you are at it, it's good to use [x.lower().strip() for x in df0]
@PawelKranzberg Do you have any idea how to lower the column names of MultiIndex
@curious_nustian - Yes, e.g.: df.index.names = [x.lower().strip() for x in df.index.names]
|
185

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
Trenton McKinney
63.2k41 gold badges170 silver badges214 bronze badges
answered Aug 13, 2016 at 10:41

Comments

58

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

data.rename(columns=str.lower)

If you're not chaining method calls, you can add inplace=True

data.rename(columns=str.lower, inplace=True)
Daniel
9,6396 gold badges69 silver badges94 bronze badges
answered Jun 22, 2016 at 9:28

Comments

8
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]
answered Jun 26, 2020 at 19:55

3 Comments

I guess it's easier to write df.columns.astype(str).str.lower() in that case but maybe a bit verbose.
These two methods have already been mentioned by the answers above. The only noteworthy comment you make is the case of numeric headers which really should be added to the existing generator answer.
@Daniel My method is better because it will not break if there are numeric columns. I used the first one for simplicity but frankly the first one should be rejected as it is prone to errors. In short, my method can be a standalone answer without referring to the first one.
0

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()
answered Jul 13, 2021 at 15:46

Comments

-1

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.

nferreira78
1,2048 silver badges23 bronze badges
answered May 10, 2022 at 0:50

1 Comment

I'd say using frame.rename(columns=str.lower, inplace=True) is much cleaner and to the point.
-1
Here is one from my end, 
# import pandas lib as needed 
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'Column One': [1, 2, 3],
'Column Two': [4, 5, 6]
})
# Function to transform column names
def transform_col_name(col_name):
 return col_name.lower().replace(' ', '_')

Rename columns using the transform_col_name function

inplace =True will make changes to df

df.rename(columns=lambda x: transform_col_name(x), inplace=True)
# Check the updated DataFrame
print(df)
answered Jan 15, 2025 at 10:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.