pandas 重命名熊猫系列中的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37164543/
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 01:13:27  来源:igfitidea点击:

Renaming an index in a Pandas Series?

pythonpandas

提问by cjm2671

I'm trying to rename the index of a series liborI have from DATE to Date:

我正在尝试重命名libor从 DATE 到 Date的系列索引:

libor.rename(index={"DATE": "Date"})

The result is unforunately the same as it went in though:

不幸的是,结果与它进入的结果相同:

            VALUE
DATE    
1986-01-02  8.12500
1986-01-03  8.12500
1986-01-06  8.18750

I was hoping that 'DATE' would become 'Date'. I'm trying to multiply it with a Dataframe with a 'Date' index, on a column-by-column basis.

我希望“日期”会变成“日期”。我正在尝试逐列将它与带有“日期”索引的数据框相乘。

How can I rename the index?

如何重命名索引?

回答by Alexander

Just change the name property of the index.

只需更改索引的名称属性即可。

libor.index.name = 'Date'

回答by MaxU

you can use rename_axis()for that:

您可以使用rename_axis()

Example:

例子:

In [310]: s
Out[310]:
Index
0    1
1    2
2    3
dtype: int64

In [312]: s = s.rename_axis('idx')

In [313]: s
Out[313]:
idx
0    1
1    2
2    3
dtype: int64