Home > Net >  How to extract a string between special character on a column dataframe in python?
How to extract a string between special character on a column dataframe in python?

Time:04-19

I have this Python Pandas DataFrame:

Municipio
São Caetano do Sul (SP)
Florianópolis (SC)
Vitória (ES)    

How to extract the term between () and turn it into:

Municipio                   UF
São Caetano do Sul (SP)     (SP)
Florianópolis (SC)          (SC)
Vitória (ES)                (ES)

CodePudding user response:

df['UF'] = df['Municipio'].str.split('(').str[1].str[:-1]

enter image description here

CodePudding user response:

You can try pandas.Series.str.extract

df['UF'] = df['Municipio'].str.extract('(\([^)]*\))')
print(df)

                 Municipio  UF
0  São Caetano do Sul (SP)  (SP)
1       Florianópolis (SC)  (SC)
2             Vitória (ES)  (ES)

CodePudding user response:

Using str.extract we can try:

df["UF"] = df["Municipio"].str.extract(r'(\([A-Z] \))')
  • Related