如何更改Pandas数据框中的特定行标签? [英] How can I change a specific row label in a Pandas dataframe?
本文介绍了如何更改Pandas数据框中的特定行标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,例如:
I have a dataframe such as:
0 1 2 3 4 5
0 41.0 22.0 9.0 4.0 2.0 1.0
1 6.0 1.0 2.0 1.0 1.0 1.0
2 4.0 2.0 4.0 1.0 0.0 1.0
3 1.0 2.0 1.0 1.0 1.0 1.0
4 5.0 1.0 0.0 1.0 0.0 1.0
5 11.4 5.6 3.2 1.6 0.8 1.0
最后一行包含平均值的位置.我想将最后一行标签重命名为"A"
,以便数据框看起来像这样:
Where the final row contains averages. I would like to rename the final row label to "A"
so that the dataframe will look like this:
0 1 2 3 4 5
0 41.0 22.0 9.0 4.0 2.0 1.0
1 6.0 1.0 2.0 1.0 1.0 1.0
2 4.0 2.0 4.0 1.0 0.0 1.0
3 1.0 2.0 1.0 1.0 1.0 1.0
4 5.0 1.0 0.0 1.0 0.0 1.0
A 11.4 5.6 3.2 1.6 0.8 1.0
我知道可以使用df.columns = . . .
完成列.但是我该如何使用特定的行标签呢?
I understand columns can be done with df.columns = . . .
. But how can I do this with a specific row label?
推荐答案
您可以使用类似于Python中的负索引来获取最后一个索引
You can get the last index using negative indexing similar to that in Python
last = df.index[-1]
然后
df = df.rename(index={last: 'a'})
如果您要寻找单线,
df.index = df.index[:-1].tolist() + ['a']
这篇关于如何更改Pandas数据框中的特定行标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文