简体   繁体   English

在熊猫数据框中提取城市、纬度和经度

[英]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?我正在按照以下方法将地址列中的所有值传递给函数,但它不起作用,有什么想法吗?

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在熊猫数据框中通过纬度/经度值分配城市名称 - Assigning City Name by Latitude/Longitude values in Pandas Dataframe 如何在熊猫数据框中将UTM转换为经度/纬度? - How to convert UTM to Longitude/Latitude in Pandas dataframe? 使用 pandas 更改纬度和经度 - Change Latitude and Longitude with pandas Pandas Dataframe:根据地理坐标(经度和纬度)连接范围内的项目 - Pandas Dataframe: join items in range based on their geo coordinates (longitude and latitude) 需要合并两个 pandas dataframe 使用两列纬度和经度 - Need to merge two pandas dataframe using two columns latitude and longitude 使用 GeoPy 计算 pandas dataframe 上的纬度/经度距离 - Calculating distance of latitude/longitude on pandas dataframe using GeoPy 使用 python 提取纬度和经度 - Extracting Latitude and Longitude using python 有没有办法在 excel 文件中包含给定城市和国家/地区的纬度和经度的 output dataframe? - Is there a way to output a dataframe containing latitude and longitude given City and Country in an excel file? 使用 City 和 State 列查找纬度和经度 - Find latitude and longitude using City and State columns 查找城市或国家/地区的经度和纬度对 - Finding pair of longitude and latitude for a city or country
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM