Skip to content
Advertisement

Pandas str.extract() regex to extract city info

I have a pandas df of addresses like this:

JavaScript

I want to extract the name of city such that expected results:

JavaScript

My code is below:

JavaScript

My results:

JavaScript

However, when the city name doesn’t end with a , it will pick up the rest of the string. If i don’t end my regex in , I won’t get the full city name in some cases. How can I resolve this?

Advertisement

Answer

One option for the example data could be matching the following words starting with a capital A-Z and optional non whitespace chars excluding a comma:

JavaScript

Regex demo

JavaScript

Output

JavaScript
Advertisement