简体   繁体   中英

Extracting city, latitude and longitude in pandas dataframe

I have an address column that contains the full location of a place eg (King Saud Road, Al Khobar 31952 Saudi Arabia). I want to extract the city in new column as well as the latitude and longitude point in another new column.

I am following the below approach to pass all values in address column to the function, but it's not working, any ideas?

code image

Use:

geolocator = Nominatim(user_agent="StackOverFlow", timeout=3)
df = pd.DataFrame({'locations': ['King Saud Road, Al Khobar 31952 Saudi Arabia',
                                 '123 Main Street, Los Angeles, CA 90034, USA']})
df['location_info'] = df.locations.apply(lambda x: geolocator.geocode(x, addressdetails=True, language='en'))
df['latitude'] = df.location_info.apply(lambda x: x.raw['lat'])
df['longitude'] = df.location_info.apply(lambda x: x.raw['lon'])
df['city'] = df.location_info.apply(lambda x: x.raw["address"]['city'])
df = df.drop(['location_info'], axis=1) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM