如何在Pandas数据框中更改单个索引值? [英] How do i change a single index value in pandas dataframe?

查看:99
本文介绍了如何在Pandas数据框中更改单个索引值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

energy.loc['Republic of Korea']

我要将index的值从"Republic of Korea"更改为"South Korea". 但是数据帧太大,无法更改每个索引值.我如何仅更改此单个值?

I want to change the value of index from 'Republic of Korea' to 'South Korea'. But the dataframe is too large and it is not possible to change every index value. How do i change only this single value?

推荐答案

您要执行以下操作:

as_list = df.index.tolist()
idx = as_list.index('Republic of Korea')
as_list[idx] = 'South Korea'
df.index = as_list

基本上,您将索引作为列表获得,更改该元素,然后替换现有索引.

Basically, you get the index as a list, change that one element, and the replace the existing index.

这篇关于如何在Pandas数据框中更改单个索引值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆