How separate latitude and longitude into two columns from dataframe using regex

After using google maps api I get a list of all business it find,enter image description here

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

Answers (2)

ashhad ullah
ashhad ullah

Reputation: 116

enter image description here

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

Cresht
Cresht

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

Related Questions