Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make pandas dataframe column headers all lowercase?

People also ask

How do you capitalize all column names in pandas?

Convert Column Names to Uppercase using str. where, df is the input dataframe and columns is the attribute to get the column labels as an Index Object. Then using the StringMethods. upper() we converted all labels to uppercase. It converted all the column labels to uppercase.

Are pandas column names case sensitive?

Answer. Yes, column names for dataframes are case sensitive. Dataframe column names are essentially string values, which are case sensitive in 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

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

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

data.rename(
    columns=unicode.lower
)

(Python 2)

or

data.rename(
    columns=str.lower
)

(Python 3)


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]