[英]Get address using Latitude and Longitude from two columns in DataFrame?
I have a dataframe with the longitude column and latitude column.我有一个带有经度列和纬度列的 dataframe。 When I try to get the address using
geolocator.reverse()
I get the error ValueError: Must be a coordinate pair or Point
当我尝试使用
geolocator.reverse()
获取地址时,出现错误ValueError: Must be a coordinate pair or Point
I can't for the life of me insert the lat and long into the reverse function without getting that error.我这辈子都无法将纬度和经度插入反向 function 而不会出现该错误。 I tried creating a tuple using
list(zip(zips['Store_latitude'], zips['Store_longitude']))
but I get the same error.我尝试使用
list(zip(zips['Store_latitude'], zips['Store_longitude']))
创建一个元组,但我得到了同样的错误。
Code:代码:
import pandas as pd
from geopy.geocoders import Nominatim
from decimal import Decimal
from geopy.point import Point
zips = pd.read_excel("zips.xlsx")
geolocator = Nominatim(user_agent="geoapiExercises")
zips['Store_latitude']= zips['Store_latitude'].astype(str)
zips['Store_longitude'] = zips['Store_longitude'].astype(str)
zips['Location'] = list(zip(zips['Store_latitude'], zips['Store_longitude']))
zips['Address'] = geolocator.reverse(zips['Location'])
What my DataFrame looks like我的 DataFrame 长什么样
Store_latitude![]() |
Store_longitude![]() |
---|---|
34.2262225 ![]() |
-118.4508349 ![]() |
34.017667 ![]() |
-118.149135 ![]() |
I think you might try with a tuple or a geopy.point.Point
before going to a list to see whether the package works all right.我认为您可以尝试使用元组或
geopy.point.Point
,然后再转到列表以查看 package 是否正常工作。
I tested just now as follows (Python 3.9.13, command line style)我刚才测试如下(Python 3.9.13,命令行风格)
import geopy
p = geopy.point.Point(51.4,3.45)
gl = geopy.geocoders.Nominatim(user_agent="my_test") # Without the user_agent it raises a ConfigurationError.
gl.reverse(p)
output: Location(Vlissingen, Zeeland, Nederland, (51.49433865, 3.415005767601362, 0.0))
output:
Location(Vlissingen, Zeeland, Nederland, (51.49433865, 3.415005767601362, 0.0))
This is as expected.这符合预期。
Maybe you should cast your dataframe['Store_latitude'] and dataframe['Store_longitude'] before/after you convert to list?也许您应该在转换为列表之前/之后投射数据框['Store_latitude'] 和数据框['Store_longitude']? They are not strings?
它们不是字符串?
More information on your dataframe and content would be required to further assist, I think.我认为需要有关您的 dataframe 和内容的更多信息以提供进一步帮助。 Good luck!
祝你好运!
EDIT: added information after OP's comments below.编辑:在下面的 OP 评论之后添加了信息。
zips = pd.read("yourexcel.xlsx")
you will get a pandas dataframe.zips = pd.read("yourexcel.xlsx")
时,您将获得 pandas dataframe。 The content of the dataframe is two columns (which will be of type Series) and each element will be a numpy.float64 (if your excel has real values as input and not strings.): You can check this using the type() command: dataframe 的内容是两列(属于 Series 类型),每个元素都是 numpy.float64(如果您的 excel 具有真实值作为输入而不是字符串。):您可以使用 type() 命令进行检查:
>>> type(zips)
<class 'pandas.core.frame.DataFrame'>
>>> type(zips['Lat'])
<class 'pandas.core.series.Series'>
>>> type(zips['Lat'][0])
<class 'numpy.float64'>
What you then do is convert these floats (=decimal numbers) to a string (=text) by performing zips[...] = zips[...].astype(str)
.然后您要做的是通过执行
zips[...] = zips[...].astype(str)
。 There is no reason to do that, because your geolocator requires numbers, not text.没有理由这样做,因为您的地理定位器需要数字,而不是文本。
So in the next block, I first create a new (empty) list.所以在下一个块中,我首先创建一个新的(空)列表。 Then i iterate over couples of lat,lon by combining your zips['Lat'] and zips['lon'] using the zip command (so the naming of zips is a bit unlucky if you don't know the zip command; it thus may be confusing you).
然后我通过使用 zip 命令组合你的 zips['Lat'] 和 zips['lon'] 来迭代几个纬度,经度(所以如果你不知道 zip 命令,那么 zips 的命名有点不吉利;它因此可能会让你感到困惑)。 But don't worry, what it does is just combining the entries of each row in the varables lat and lon.
但别担心,它所做的只是组合变量 lat 和 lon 中每一行的条目。 Within the for-each loop, I append the result of the geolocator lookup.
在 for-each 循环中,我 append 地理定位器查找的结果。 Note that the argument of the reverse command is a tuple (lat,lon), so the complete syntax is reverse( (lat,lon) ).
请注意,reverse 命令的参数是一个元组 (lat,lon),因此完整的语法是 reverse((lat,lon))。 Instead of (lat,lon), you could also have created a Point as in my original example.
除了 (lat,lon),您还可以像我原来的示例中那样创建一个点。 But that is not necessary imo.
但这不是必要的。 (note: for brevity I just write 'Lat' and 'Lon' instead of your Store...).
(注意:为简洁起见,我只写了“Lat”和“Lon”,而不是你的商店......)。 Finally, assign the result list as a new column in your zip pandas dataframe.
最后,将结果列表分配为您的 zip pandas dataframe 中的新列。
import geopy as gp
# instiate a geolocator
gl = gp.geocoders.Nominatim(user_agent="my_test")
locations = [] # Create empty list
# For loop over each couple of lat, lon
for lat,lon in zip(zips['Lat'], zips['Lon']):
locations.append(gl.reverse((lat,lon))
# Add extra column to your pandas table (address will be the column name)
zips = zips.assign(address=locations)
One thing you still may want, is just have the text string instead of the complete geopy.Location() string in your table.您仍然可能想要的一件事是,在您的表中只包含文本字符串而不是完整的 geopy.Location() 字符串。 To get that you write the for loop with this small modification ([0] as the first element of the Location object).
为此,您需要对 for 循环进行少量修改([0] 作为 Location 对象的第一个元素)。 Note that this won't work if the result of the lookup of a given row is empty (None).
请注意,如果给定行的查找结果为空(无),这将不起作用。 Then the [0] will raise an error.
然后 [0] 将引发错误。
# For loop over each couple of lat, lon
for lat,lon in zip(zips['Lat'], zips['Lon']:
locations.append(gl.reverse((lat,lon)[0])
I hope this gets you going!我希望这能让你前进!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.