简体   繁体   中英

Extracting a specific part of a string in python

I am trying to extract a specific part of a string that is within a pandas series.

For example:

energy['Country'] 

Gives me:

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

I want to change 'Bolivia (Plurinational State of)' to 'Bolivia'.

My failed attempt is:

pattern = “(.*?)”
list = [re.sub(pattern, '', i) for i in energy['Country']]
energy['Country'] = list

Could anyone give me any advice on how I can amend my code to make this work??

Do this:

df['Country'] = df['Country'].str.replace(r"\(.*\)","")

Example on sample 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM