How do I change a single index value in pandas dataframe?

user517696 picture user517696 · Nov 4, 2016 · Viewed 80.2k times · Source
energy.loc['Republic of 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?

Answer

Batman picture Batman · Nov 4, 2016

You want to do something like this:

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.