本文介绍了如何使Pandas DataFrame列标题全部小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使我的pandas数据框中的所有列标题都小写

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

如果我有

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
....

我想通过以下操作将XRAT更改为xrat:

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

data.headers.lowercase()

这样我得到:

  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.

推荐答案

您可以这样做:

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

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

示例:

>>> 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

这篇关于如何使Pandas DataFrame列标题全部小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:33