我的任务是删除括号中的任何内容,并删除后跟国家名称的任何数字。改变几个国家的名字。在
例如。 玻利维亚(多民族国)应为“玻利维亚” 瑞士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语法一定有问题,怎么解决这个冲突呢?为什么会这样?在
问题是您需要} 来替换
regex=True
^{substrings
:^{pr2}$
相关问题 更多 >
编程相关推荐