简体   繁体   English

有没有办法在 excel 文件中包含给定城市和国家/地区的纬度和经度的 output dataframe?

[英]Is there a way to output a dataframe containing latitude and longitude given City and Country in an excel file?

I have an excel file containing two columns.我有一个包含两列的 excel 文件。 The first column is "City" and the second column is "Country".第一列是“城市”,第二列是“国家”。 I want my python code to loop through each row and find the latitude and longitude for each row.我希望我的 python 代码遍历每一行并找到每一行的纬度和经度。 The python code creates two new columns "Latitude" and "Longitude" as desired but is returning None for all values. python 代码根据需要创建了两个新列“Latitude”和“Longitude”,但对所有值都返回 None。 Any help is appreciated.任何帮助表示赞赏。

import pandas as pd
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="Your_Name")

df = pd.read_excel("location.xlsx")

longitude = []
latitude = []

city = df["City"]
country = df["Country"]

for i in df.index:

    loc = geolocator.geocode(city + ',' + country)

    if loc is None:
        latitude.append(None)
        longitude.append(None)
    else:
        latitude.append(loc.latitude)
        longitude.append(loc.longitude)

df["Longitude"] = longitude
df["Latitude"] = latitude

print(df)

With the given dataframe df below:使用下面给定的 dataframe df

import pandas as pd

data = {"City": ["London", "Berlin", "Madrid", "Rome", 
                   "Paris", "Vienna", "Bucharest", "Hamburg", 
                   "Budapest", "Warsaw", "Barcelona", 
                   "Munich", "Milan"],
        "Country": ["England", "Germany", "Spain", "Italy",
                      "France", "Austria", "Romania", 
                      "Germany", "Hungary", "Poland", "Spain",
                      "Germany", "Italy"]
       }

df = pd.DataFrame(data)

You can get the lat/long of the couple City/Country by using geolocator.geocode :您可以使用geolocator.geocode获取城市/国家/地区的纬度/经度:

from geopy.geocoders import Nominatim

out = (
        df.assign(Geocodes= (df['City'] + ", " + df['Country']).apply(geolocator.geocode))
          .assign(Latitude = lambda x: [g.latitude for g in x['Geocodes']],
                  Longitude= lambda x: [g.longitude for g in x['Geocodes']])
          .drop(columns='Geocodes')
      )

# Output: #Output:

print(out)

         City  Country   Latitude  Longitude
0      London  England  51.507322  -0.127647
1      Berlin  Germany  52.517037  13.388860
2      Madrid    Spain  40.416705  -3.703582
3        Rome    Italy  41.893320  12.482932
4       Paris   France  48.858890   2.320041
5      Vienna  Austria  48.208354  16.372504
6   Bucharest  Romania  44.436141  26.102720
7     Hamburg  Germany  53.550341  10.000654
8    Budapest  Hungary  47.497994  19.040359
9      Warsaw   Poland  52.231958  21.006725
10  Barcelona    Spain  41.382894   2.177432
11     Munich  Germany  48.137108  11.575382
12      Milan    Italy  45.464194   9.189635

You missed indexing the series city and country.您错过了索引城市和国家系列。

Just try loc = geolocator.geocode(city[i] + ',' + country[i]) in your code.只需在您的代码中尝试loc = geolocator.geocode(city[i] + ',' + country[i]) It's working for me.它对我有用。

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

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