使用 python/pandas 中范围内的数字重命名列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42847441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:12:50  来源:igfitidea点击:

Renaming columns using numbers from a range in python/pandas

pythonpython-3.xpandas

提问by elanamig

I'm stuck with the following situation. I'm pretty sure I'm missing something simple, but I tried a lot of suggestions here and at other sites, and haven't found what I'm looking for.

我被以下情况困住了。我很确定我错过了一些简单的东西,但是我在这里和其他网站上尝试了很多建议,但没有找到我要找的东西。

I have a dataframe with a lot of randomly named columns (courtesy of provided csv file). I would like to rename these columns using digits from the range function.

我有一个包含许多随机命名列的数据框(由提供的 csv 文件提供)。我想使用 range 函数中的数字重命名这些列。

Since I'm renaming all columns, I could do it directly using

由于我要重命名所有列,因此可以直接使用

df.columns = [str(x) for x in range(1,2000)]

However, hypothetically, could I do it through the rename() function? Maybe using a lambda? I have tried many different variations, but I'm getting all sorts of errors. I'm looking for the syntax to give me the equivalent of

但是,假设我可以通过 rename() 函数来完成吗?也许使用lambda?我尝试了许多不同的变体,但我遇到了各种各样的错误。我正在寻找语法给我相当于

 df.rename(columns= (str(x) for x in range(1,2000)))

where rename assigns the name to the columns sequentially based on the given range. The above does't work. But is there a way to make it work?

其中 rename 根据给定的范围将名称按顺序分配给列。以上是行不通的。但是有没有办法让它发挥作用?

Thank you!

谢谢!

回答by mechanical_meat

You can pass a dictto rename's columnskwarg:

您可以通过 adict来重命名columnskwarg:

df.rename(columns={x:y for x,y in zip(df.columns,range(0,len(df.columns)))})

That will take:

这将需要:

>>> df
   ID1  ID2 POS1 POS2     TYPE TYPEVAL
1    A  001    1    5    COLOR     RED
2    A  001    1    5   WEIGHT    50KG
3    A  001    1    5   HEIGHT   160CM
4    A  002    6   19   FUTURE     YES
5    A  002    6   19  PRESENT      NO
6    B  001   26   34   COLOUR    BLUE
7    B  001   26   34   WEIGHT    85KG
8    B  001   26   34   HEIGHT   120CM
9    C  001   10   13   MOBILE   NOKIA
10   C  001   10   13   TABLET    ASUS

And give you:

并给你:

>>> df.rename(columns={x:y for x,y in zip(df.columns,range(0,len(df.columns)))})
    0    1   2   3        4      5
1   A  001   1   5    COLOR    RED
2   A  001   1   5   WEIGHT   50KG
3   A  001   1   5   HEIGHT  160CM
4   A  002   6  19   FUTURE    YES
5   A  002   6  19  PRESENT     NO
6   B  001  26  34   COLOUR   BLUE
7   B  001  26  34   WEIGHT   85KG
8   B  001  26  34   HEIGHT  120CM
9   C  001  10  13   MOBILE  NOKIA
10  C  001  10  13   TABLET   ASUS

回答by Joe T. Boka

If you just want to rename the columns using numbers, this is probably the easiest way to do it:

如果您只想使用数字重命名列,这可能是最简单的方法:

df.columns = np.arange(len(df.columns))

Demo:

演示:

df = pd.DataFrame({'A':['a', 'b', 'c'], 'B': ['d','e','f'], 'C': ['g','h','i']})
print(df)
   A  B  C
0  a  d  g
1  b  e  h
2  c  f  i

Renaming the columns:

重命名列:

df.columns = np.arange(len(df.columns))
print(df)
   0  1  2
0  a  d  g
1  b  e  h
2  c  f  i