我的任务是删除括号中的任何内容,并删除任何数字后跟国家/地区名称。改变一些国家的名字。pandas.replace与str.replace正则表达式冲突。代码顺序
例如 玻利维亚(多民族国)'应该'玻利维亚' 瑞士17'应该是'瑞士'。
我的原代码顺序为:
dict1 = {
"Republic of Korea": "South Korea",
"United States of America": "United States",
"United Kingdom of Great Britain and Northern Ireland": "United Kingdom",
"China, Hong Kong Special Administrative Region": "Hong Kong"}
energy['Country'] = energy['Country'].replace(dict1)
energy['Country'] = energy['Country'].str.replace(r' \(.*\)', '')
energy['Country'] = energy['Country'].str.replace('\d+', '')
energy.loc[energy['Country'] == 'United States']
的str.replace
部分工作正常。任务已完成。 当我使用最后一行来检查我是否成功更改了国家/地区名称。此原始代码不起作用。但是,如果我更改代码的顺序为:
energy['Country'] = energy['Country'].str.replace(r' \(.*\)', '') energy['Country'] = energy['Country'].str.replace('\d+', '') energy['Country'] = energy['Country'].replace(dict1)
然后,它成功地改变了国家名称。 因此,我的Regex语法一定有什么问题,如何解决这个冲突?这是为什么发生?
似乎有没有冲突。首先需要删除不必要的字符串部分,然后用字典替换。首先不起作用,因为没有匹配的字典键。 – jezrael
对不起,我不明白,我所做的只是改变能量['Country'] = energy ['Country']的顺序。replace(dict1)Line。在弦乐部分没有编辑任何内容。为什么突然变得有效? – Dylan
请检查我的答案 – jezrael