Reputation: 149
After using google maps api I get a list of all business it find,
on the column geometry I get this data for each business:
{'location': {'lat': 39.7904306, 'lng': -86.1631634}, 'viewport': {'northeast': {'lat': 39.79635710000001, 'lng': -86.15916770000001}, 'southwest': {'lat': 39.78564629999999, 'lng': -86.16786649999999}}}
I need to separate latitude and longitude into two separate columns one for each coordinate, after trying regex:
import re
Health_centers['Venue Latitude'] = Health_centers['geometry'].str.extract(r"[^}]*")
I only get to extract
{'location': {'lat': 39.7904306, 'lng': -86.1631634
there is a way to combine strip and regex to do it with one code line?
Upvotes: 1
Views: 882
Reputation: 116
Hey, I tried to write a regex for you 'lat':[^0-9]-?[0-9]*[.]-?[0-9]+|'lng':[^0-9]-?[0-9]*[.]-?[0-9]+
Like this you can get the desired output but I think you can just simply parse the JSON and get the lat and long properties easily.
Upvotes: 1
Reputation: 1040
It's possible that I am missing something, but does this line suffice? It makes a list containing both lat
and lng
. You could also get them separately if that is better.
list(Health_centers['geometry']['location'].values())
If the data is actually a string, this would do the trick.
import json
list(json.loads(Health_centers['geometry'])['location'].values())
Upvotes: 1