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

PythonPandasData Analysis

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

@EdChum's solution looks good. Here's one using rename, which would replace all these values in the index.

energy.rename(index={'Republic of Korea':'South Korea'},inplace=True)

Here's an example

>>> example = pd.DataFrame({'key1' : ['a','a','a','b','a','b'],
           'data1' : [1,2,2,3,nan,4],
           'data2' : list('abcdef')})
>>> example.set_index('key1',inplace=True)
>>> example
      data1 data2
key1             
a       1.0     a
a       2.0     b
a       2.0     c
b       3.0     d
a       NaN     e
b       4.0     f

>>> example.rename(index={'a':'c'}) # can also use inplace=True
      data1 data2
key1             
c       1.0     a
c       2.0     b
c       2.0     c
b       3.0     d
c       NaN     e
b       4.0     f

Solution 2 - Python

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.

Solution 3 - Python

Try This

df.rename(index={'Republic of Korea':'South Korea'},inplace=True)

Solution 4 - Python

If you have MultiIndex DataFrame, do this:

# input DataFrame
import pandas as pd
t = pd.DataFrame(data={'i1':[0,0,0,0,1,1,1,1,2,2,2,2],
                       'i2':[0,1,2,3,0,1,2,3,0,1,2,3],
                       'x':[1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.]})
t.set_index(['i1','i2'], inplace=True)
t.sort_index(inplace=True)

# changes index level 'i1' values 0 to -1
t.rename(index={0:-1}, level='i1', inplace=True)

Solution 5 - Python

Here's another good one, using replace on the column.

df.reset_index(inplace=True)
df.drop('index', axis = 1, inplace=True)
df["Country"].replace("Republic of Korea", value="South Korea", inplace=True)
df.set_index("Country", inplace=True)

Solution 6 - Python

Here's another idea based on set_value

df = df.reset_index()
df.drop('index', axis = 1, inplace=True)
index = df.index[df["Country"] == "Republic of Korea"]
df.set_value(index, "Country", "South Korea")
df = df.set_index("Country")
df["Country"] = df.index

Solution 7 - Python

This seems to work too:

energy.index.values[energy.index.tolist().index('Republic of Korea')] = 'South Korea'

No idea though whether this is recommended or discouraged.

Solution 8 - Python

We can use rename function to change row index or column name. Here is the example,

Suppose data frame is like given below,

       student_id     marks
index
  1        12          33
  2        23          98
  • To change index 1 to 5

we will use axis = 0 which is for row

df.rename({ 1 : 5 }, axis=0)

df refers to data frame variable. So, output will be like

       student_id     marks
index
  5        12          33
  2        23          98
  • To change column name

we will have to use axis = 1

df.rename({ "marks" : "student_marks" }, axis=1)

so, changed data frame is

       student_id     student_marks
index
  5        12              33
  2        23              98

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser517696View Question on Stackoverflow
Solution 1 - PythonErnestScribblerView Answer on Stackoverflow
Solution 2 - PythonBatmanView Answer on Stackoverflow
Solution 3 - PythonAbdul RafayView Answer on Stackoverflow
Solution 4 - PythonS.VView Answer on Stackoverflow
Solution 5 - PythonAndrea CView Answer on Stackoverflow
Solution 6 - PythonAndrea CView Answer on Stackoverflow
Solution 7 - PythonmpeliView Answer on Stackoverflow
Solution 8 - Pythonbhargav3vediView Answer on Stackoverflow