我需要替换一些在数据帧的' country‘列中有'()’或数字的国家名称。例如,“多民族玻利维亚国”应改为“多民族玻利维亚国”。“Switzerland17”应为“瑞士”。
我正在使用下面的代码
df3['Country'] = df3['Country'].str.replace(r'[^(][\w]*[)]','')
df3['Country'] = df3['Country'].str.replace(r'[\d]*','')我哪里出错了,你能帮我吗?
发布于 2020-12-14 19:39:55
\s*(?:\([^()]*\)|\d+)说明
\s*匹配来自(的非捕获组\([^()]*\)|\d+匹配0+空白字符..match ..) or match 1+ digits)关闭非捕获组df3['Country'] = df3['Country'].str.replace(r'\s*(?:\([^()]*\)|\d+)', '')输出
Country
0 Bolivia
1 Switzerland发布于 2020-12-14 20:08:54
你应该使用
df3['Country'].str.replace(r"\s*(?:\d+|\([^()]*\))","").str.strip()请参阅regex demo。详细信息:
\s* -零个或多个whitespaces(?:\d+|\([^()]*\)) -一个或多个数字,或者(,然后是除(和)之外的零个或多个字符,最后是)如果匹配恰好在开头,并且后面跟着空格,则.str.strip()是必需的。
请参阅Pandas测试:
>>> import pandas as pd
>>> df3 = pd.DataFrame({'Country':['Bolivia (Plurinational State of)','Switzerland17','(Republic of) Korea']})
>>> df3['Country'].str.replace(r"\s*(?:\d+|\([^()]*\))","").str.strip()
0 Bolivia
1 Switzerland
2 Korea
Name: Country, dtype: object发布于 2020-12-14 19:41:09
我将使用以下模式:‘(^)|[\d.]’|字符使您可以在一行中使用多个模式。
df = pd.DataFrame({'Country':['Bolivia (Plurinational State of)','Switzerland17']})原始df:
Country
0 Bolivia (Plurinational State of)
1 Switzerland17建议的代码:
df['Country'] = df['Country'].str.replace(r'\([^)]*\)|[\d.*]','',regex=True)输出:
Country
0 Bolivia
1 Switzerlandhttps://stackoverflow.com/questions/65288351
复制相似问题