mercredi 3 février 2021

Repeat pattern using python regex

Well, I'm cleaning a dataset, using Pandas. I have a column called "Country", where different rows could have numbers or other information into parenthesis and I have to remove them, for example: Australia1, Perú (country), 3Costa Rica, etc. To do this, I'm getting the column and I make a mapping over it.

pattern = "([a-zA-Z]+[\s]*[a-aZ-Z]+)(?:[(]*.*[)]*)"
df['Country'] = df['Country'].str.extract(pattern)

But I have a problem with this regex, I cannot match names as "United States of America", because it only takes "United ". How can I repeat unlimited the pattern of the fisrt group to match the whole name?
Thanks!

Aucun commentaire:

Enregistrer un commentaire