如何重命名 Python Pandas 中的索引行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43641855/
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
How do I rename an index row in Python Pandas?
提问by ScoutEU
I see how to rename columns, but I want to rename an index (row name) that I have in a data frame.
我了解如何重命名列,但我想重命名数据框中的索引(行名称)。
I had a table with 350 rows in it, I then added a total to the bottom. I then removed every row except the last row.
我有一个包含 350 行的表格,然后我在底部添加了总数。然后我删除了除最后一行之外的每一行。
-------------------------------------------------
| | A | B | C |
-------------------------------------------------
| TOTAL | 1243 | 423 | 23 |
-------------------------------------------------
So I have the row called 'Total', and then several columns. I want to rename the word 'Total' to something else.
所以我有一个名为“总计”的行,然后是几列。我想将“总计”一词重命名为其他名称。
Is this even possible?
这甚至可能吗?
Many thanks
非常感谢
回答by SGhaleb
Easy as this...
就这么简单...
df.index.name = 'Name'
回答by mforez
You could use a dictionary structure with rename()
, for example,
rename()
例如,您可以使用带有 的字典结构,
In [1]: import pandas as pd
df = pd.Series([1, 2, 3])
df
Out[1]: 0 1
1 2
2 3
dtype: int64
In [2]: df.rename({1: 3, 2: 'total'})
Out[2]: 0 1
3 2
total 3
dtype: int64