[英]Extracting a specific part of a string in python
我正在尝试提取 pandas 系列中的字符串的特定部分。
例如:
energy['Country']
给我:
27 Aruba
28 Australia1
29 Austria
30 Azerbaijan
31 Bahamas
32 Bahrain
33 Bangladesh
34 Barbados
35 Belarus
36 Belgium
37 Belize
38 Benin
39 Bermuda
40 Bhutan
41 Bolivia (Plurinational State of)
42 Bonaire, Sint Eustatius and Saba
我想将“玻利维亚(多民族 State of)”更改为“玻利维亚”。
我失败的尝试是:
pattern = “(.*?)”
list = [re.sub(pattern, '', i) for i in energy['Country']]
energy['Country'] = list
谁能给我任何关于如何修改我的代码以使其工作的建议?
做这个:
df['Country'] = df['Country'].str.replace(r"\(.*\)","")
示例 dataframe 示例:
In [91]: df
Out[91]:
Country
0 Aruba
1 Australia1
2 Bolivia (Plurinational State of)
In [93]: df['Country'] = df['Country'].str.replace(r"\(.*\)","")
In [94]: df
Out[94]:
Country
0 Aruba
1 Australia1
2 Bolivia
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.