我正在尝试提取熊猫系列中字符串的特定部分。
例如:
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
我想将“玻利维亚多民族国”更改为“玻利维亚”。
我失败的尝试是:
pattern = “(.*?)”
list = [re.sub(pattern, '', i) for i in energy['Country']]
energy['Country'] = list
谁能给我任何有关如何修改代码以使其正常工作的建议!?
做这个:
df['Country'] = df['Country'].str.replace(r"\(.*\)","")
示例数据框示例:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句