Python: Extract function with Pandas column ignoring regex that i've verified on regexr.com

I have a column in a pandas data table that contains a string with the city and coordinates. I want to extract the coordinates

It should be a relatively simple exercise:

enter image description here

Here is the code that i'm using to do the extract:

p = r'(?P<latitude>-?\d+\.\d+).*?(?P<longitude>-?\d+\.\d+)'
data[['latitude', 'longitude']] = data['geocode_result'].str.extract(p, expand=True)
data

But as you can see from my column, in the screenshot, it's listing NaN

Here is the regex being verified that it's capturing:

enter image description here

Am I missing something obvious? I just want to grab the coordinates from geocode_result

Thanks

Upvotes: 1

Views: 256

Answers (1)

cryptoplex
cryptoplex

Reputation: 1253

Your example is working for me, but the extracted type is a string which makes me suspect that whatever code you're using to generate the screenshot is getting tripped up by the value not being a float.

Try converting the values to float:

data[['latitude', 'longitude']] = data['geocode_result'].str.extract(p, expand=True).astype(float)

Upvotes: 2

Related Questions