在pandas数据帧中提取城市、纬度和经度



我有一个地址列,其中包含一个地方的完整位置,例如(沙特阿拉伯Al Khobar国王路31952(。我想在新列中提取城市,以及在另一个新列中的纬度和经度点。

我按照下面的方法将地址列中的所有值传递给函数,但它不起作用,有什么想法吗?

代码图像

使用:

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) 

最新更新