Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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.

1 Answer

0 votes
by (41.4k points)

  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

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

 

 

Browse Categories

...