Organizing latitude and longitude into separate columns using Pandas and Geopy to geocode a list of addresses

huangapple go评论61阅读模式
英文:

Organizing latitude and longitude into separate columns using Pandas and Geopy to geocode a list of addresses

问题

我已在Python中设置了一个地理编码程序,用于确定CSV文件中地址列表的坐标。我已经设置好并进行了地理编码,但我正在努力弄清楚如何将纬度和经度放入单独的列中。我已创建一个“point”列,其中使用Geopy自述文件中的lambda函数存储地理编码点:

df['point'] = df['location'].apply(lambda loc: tuple(loc.point) if loc else None)

现在数据看起来像这样:(27.9477595, -82.458444, 0.0)

尽管对我来说似乎很简单,但我似乎无法将上面示例数据中的每个坐标放入单独的列中。我希望能够有一个纬度列和一个经度列,以便在Folium中更好地进行转换。

再次强调,这可能很简单,希望有人能帮助我。另外,我还没有研究Folium在位置数据方面可以接受什么,只是假设我需要将其转换为单独的坐标。如果对Folium的了解使我的问题无关紧要,那也可以,但我不确定。

我希望有一个纬度列和一个经度列,而不是在单个列中的合并数据。

用于运行地理编码的当前代码:

tqdm.pandas()
geocode = RateLimiter(geolocator.geocode, swallow_exceptions=True)
df_trainers['gcode'] = df_trainers['full_address'].progress_apply(geocode)
df_trainers['point'] = df_trainers['gcode'].apply(lambda loc: tuple(loc.point) if loc else None)
英文:

I have set up a geocoding procedure in Python to determine the coordinates of a list of addresses in a csv file. I have it all set up and geocoding, however, I am struggling to figure out how to put the latitude and longitude into separate columns. I have created a 'point' column where the geocode point gets stored using a lambda as per the Geopy readme docs:

df['point'] = df['location'].apply(lambda loc: tuple(loc.point) if loc else None)

Right now the data looks like this: (27.9477595, -82.458444, 0.0)

Although it seems straight forward to me, I can't seem to be able to put each coordinate in the example data above into separate columns. I want to be able to have a latitude column and a longitude column so it can be better translated in Folium.

Again, this is likely straight forward and hopefully someone can help me out. Conversely, I have not looked into what Folium can accept in terms of location data and only assuming I need to get it into separate coordinates. If insight into Folium makes my question moot, than that is fine as well but I am not sure.

I am expecting a column for latitude and a column for longitude instead of the consolidated data in a single column.

Current code to run the geocoding:

    tqdm.pandas()
    geocode = RateLimiter(geolocator.geocode, swallow_exceptions=True)
    df_trainers['gcode'] = df_trainers['full_address'].progress_apply(geocode)
    df_trainers['point'] = df_trainers['gcode'].apply(lambda loc: tuple(loc.point) if loc else None)

答案1

得分: 0

你可以使用以下方法将你的坐标列分成三列:

df_trainers[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df_trainers['point'].tolist(), index=df_trainers.index)
英文:

You can use the following to turn your coordinate column in to three columns:

df_trainers[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df_trainers['point'].tolist(), index=df_trainers.index)

huangapple
  • 本文由 发表于 2023年2月7日 04:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定