热线电话:13121318867

登录
2019-02-18 阅读量: 834
为什么str.replace(在索引上)给出了Error

我试图使用下面的代码替换国家/地区名称中的括号,其中Country是DataFrame的索引:

energy['Country'] = energy['Country'].str.replace(r"\s+\(.*\)","")

我在这里和那里尝试过变化,但无论我做什么,我都会收到以下错误:

KeyError Traceback (most recent call last)

/opt/conda/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)

2133 try:

-> 2134 return self._engine.get_loc(key)

2135 except KeyError:

接下来是这样的:

KeyError: 'Country'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)

<ipython-input-45-740ea96e825f> in <module>()

23

24 #energy['Country'] = energy['Country'].str.replace("A","B")

---> 25 energy['Country'] = energy['Country'].str.replace(r"\s+\(.*\)","")

26

27 #energy['Country'] = energy['Country']

解决办法:

如果索引中包含“国家/地区”,则无法使用df['Country']语法访问它。这仅适用于表列。但是你有其他选择。

我使用了以下测试DataFrame来保持简单。

df = pd.DataFrame([('abb', 1, 2), ('abc', 2, 4), ('abd', 3, 7), ('abe', 4, 8), ('abg', 5, 6), ('abh', 6, 3)], columns=['Country', 'b', 'c'])

如果'Country'在索引(和单级索引)中,您可以执行如下替换。注意,这不适用于MultiIndex。

df = df.set_index('Country')

df.index = df.index.str.replace(r"a","")

或者,您可以使用.reset_index将所有内容移出索引并返回列。然后,您可以进行索引编制。

df = df.set_index(['Country', 'b']) # Move 2 columns into the index.

df = df.reset_index() # Country & b are now back out of the index, as a normal columns.

df['Country'] = df['Country'].str.replace(r"a","") # Normal indexing works.

在这两种情况下,您都应该获得以下输出

Country b c

0 bb 1 2

1 bc 2 4

2 bd 3 7

3 be 4 8

4 bg 5 6

5 bh 6 3

25.7508
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子