Search code examples
pythonpandasdata-analysis

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


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?


Solution

  • 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.