如何在pandas框架中更改单个索引值?

sr4lhrrt  于 8个月前  发布在  其他
关注(0)|答案(9)|浏览(68)
energy.loc['Republic of Korea']

字符串
我想将索引的值从“Republic of Korea”更改为“South Korea”。但是索引框太大,无法更改每个索引值。如何仅更改此单个值?

6xfqseft

6xfqseft1#

@EdChum的解决方案看起来不错。这里有一个使用重命名的解决方案,它将替换索引中的所有这些值。

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

字符串
这里有一个例子

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

vngu2lb8

vngu2lb82#

你想做这样的事情:

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

字符串
基本上,你得到的索引是一个列表,改变一个元素,然后替换现有的索引。

3z6pesqy

3z6pesqy3#

试试这个

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

字符串

jdzmm42g

jdzmm42g4#

如果你有MultiIndex DataFrame,请这样做:

# 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)

字符串

piah890a

piah890a5#

这是另一个很好的例子,在列上使用replace。

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)

字符串

mznpcxlj

mznpcxlj6#

下面是基于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

字符串

pxiryf3j

pxiryf3j7#

我们可以使用rename函数来更改行索引或列名。下面是示例:
假设 Dataframe 如下所示,

student_id     marks
index
  1        12          33
  2        23          98

字符串

  • 将索引1更改为5

我们将使用axis = 0表示行

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


df表示 Dataframe 变量。因此,输出将如下所示

student_id     marks
index
  5        12          33
  2        23          98

  • 更改列名

我们必须使用axis = 1

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


因此,改变的 Dataframe

student_id     student_marks
index
  5        12              33
  2        23              98

rm5edbpk

rm5edbpk8#

这似乎也起作用:

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

字符串
我不知道这是建议还是不鼓励。

sd2nnvve

sd2nnvve9#

如果你的DataFrameRangeIndex(参见docs),像这样的单行df:

>>> df.index
RangeIndex(start=0, stop=1, step=1)

>>> df.index.values
array([0])

字符串
.您可以通过以下方式更改索引值:

new_index=42
df.index = pd.RangeIndex(start=new_index, stop=new_index+1, step=1)


为了达到预期效果:

>>> df.index
RangeIndex(start=42, stop=43, step=1)

>>> df.index.values
array([42])

相关问题