[英]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).我有一个地址列,其中包含一个地方的完整位置,例如(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?我正在按照以下方法将地址列中的所有值传递给函数,但它不起作用,有什么想法吗?
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.