使用 .astype(str) 将具有地址和坐标的列转换为字符串会删除坐标
Converting a column with address and coordinates to string with .astype(str) drops the coordinates
我正在使用 geopy 包来搜索坐标的地址,该列返回匹配的地址和坐标
我只想获取坐标
这是一个向您展示其工作原理的测试:
# Test to see if response is obtained for easy address
location = geolocator.geocode("175 5th Avenue NYC", timeout=10)
print((location.latitude, location.longitude))
>>> (40.7410861, -73.9896298241625)
在我的代码中,我有一个包含城市的 CSV,然后使用 geopy 包进行查找
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
我只想从这里获取坐标
尽管正则表达式很好,但使用提取似乎不起作用,只是 returns NaN 值:
p = r'(?P<latitude>-?\d+\.\d+)?(?P<longitude>-?\d+\.\d+)'
data[['g_latitude', 'g_longitude']] = data['geocode_result2'].str.extract(p, expand=True)
data
我感觉这些问题是由于列中的 geopy 返回的对象引起的
正则表达式是正确的,已在 Regexr.com 上验证:
我已经尝试将列转换为字符串,但是坐标被删除了?!
data['geocode_result2'] = (data['geocode_result2']).astype(str)
data
有人可以帮忙吗?非常感谢
虚拟数据:
我要从中提取坐标的列是 geocode_result2 或 geocode_result
geocode_result2
1 (Agona Swedru, Central Region, Ghana, (5.534454, -0.700763))
2 (Madina, Adenta, Greater Accra Region, PMB 107 MD, Ghana, (5.6864962, -0.1677052))
3 (Ashaiman, Greater Accra Region, TM3 8AA, Ghana, (5.77329565, -0.110766330148484))
获取坐标的最终代码:
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
x = data['geocode_result']
data.dropna(subset=['geocode_result'], inplace=True)
data['g_latitude'] = data['geocode_result'].apply(lambda loc: loc.latitude)
data['g_longitude'] = data['geocode_result'].apply(lambda loc: loc.longitude)
data
您可以尝试使用 .apply
和 .str
例如:
def getLatLog(d):
try:
return re.findall(r"\d+\.\d+", d)
except:
return [None, None]
df['g_latitude'], df['g_longitude'] = df["geocode_result2"].apply(lambda x: getLatLog(x)).str
print(df["g_latitude"])
print(df["g_longitude"])
输出:
0 5.534454
1 5.6864962
2 5.77329565
Name: g_latitude, dtype: object
0 0.700763
1 0.1677052
2 0.110766330148484
Name: g_longitude, dtype: object
geolocator.geocode
returns Location
对象而不是字符串(尽管它的字符串表示实际上包含您试图解析的 lat/long ),所以 lat/long 可以通过分别访问 location.latitude
/ location.longitude
属性来检索。
# Make geocoding requests
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
# Extract lat/long to separate columns
data['g_latitude'] = data['geocode_result'].apply(lambda loc: loc.latitude)
data['g_longitude'] = data['geocode_result'].apply(lambda loc: loc.longitude)
Result
(由于缺乏声誉,我无法发表评论,所以我在这里回答坐标下降混乱)。
str(location)
returns 一个文本地址(没有坐标),但是 repr(location)
returns 以下格式的字符串(包括坐标):
Location(%(address)s, (%(latitude)s, %(longitude)s, %(altitude)s))
打印 data
时看到的内容使用 repr
(pandas 似乎为了简洁而删除了前导 Location
类型),因此您可以看到坐标。但是当列转换为 str
时,它使用 str
表示,不包括坐标。这就是全部的魔力。
我正在使用 geopy 包来搜索坐标的地址,该列返回匹配的地址和坐标
我只想获取坐标
这是一个向您展示其工作原理的测试:
# Test to see if response is obtained for easy address
location = geolocator.geocode("175 5th Avenue NYC", timeout=10)
print((location.latitude, location.longitude))
>>> (40.7410861, -73.9896298241625)
在我的代码中,我有一个包含城市的 CSV,然后使用 geopy 包进行查找
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
我只想从这里获取坐标
尽管正则表达式很好,但使用提取似乎不起作用,只是 returns NaN 值:
p = r'(?P<latitude>-?\d+\.\d+)?(?P<longitude>-?\d+\.\d+)'
data[['g_latitude', 'g_longitude']] = data['geocode_result2'].str.extract(p, expand=True)
data
我感觉这些问题是由于列中的 geopy 返回的对象引起的
正则表达式是正确的,已在 Regexr.com 上验证:
我已经尝试将列转换为字符串,但是坐标被删除了?!
data['geocode_result2'] = (data['geocode_result2']).astype(str)
data
有人可以帮忙吗?非常感谢
虚拟数据:
我要从中提取坐标的列是 geocode_result2 或 geocode_result
geocode_result2
1 (Agona Swedru, Central Region, Ghana, (5.534454, -0.700763))
2 (Madina, Adenta, Greater Accra Region, PMB 107 MD, Ghana, (5.6864962, -0.1677052))
3 (Ashaiman, Greater Accra Region, TM3 8AA, Ghana, (5.77329565, -0.110766330148484))
获取坐标的最终代码:
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
x = data['geocode_result']
data.dropna(subset=['geocode_result'], inplace=True)
data['g_latitude'] = data['geocode_result'].apply(lambda loc: loc.latitude)
data['g_longitude'] = data['geocode_result'].apply(lambda loc: loc.longitude)
data
您可以尝试使用 .apply
和 .str
例如:
def getLatLog(d):
try:
return re.findall(r"\d+\.\d+", d)
except:
return [None, None]
df['g_latitude'], df['g_longitude'] = df["geocode_result2"].apply(lambda x: getLatLog(x)).str
print(df["g_latitude"])
print(df["g_longitude"])
输出:
0 5.534454
1 5.6864962
2 5.77329565
Name: g_latitude, dtype: object
0 0.700763
1 0.1677052
2 0.110766330148484
Name: g_longitude, dtype: object
geolocator.geocode
returns Location
对象而不是字符串(尽管它的字符串表示实际上包含您试图解析的 lat/long ),所以 lat/long 可以通过分别访问 location.latitude
/ location.longitude
属性来检索。
# Make geocoding requests
data['geocode_result'] = [geolocator.geocode(x, timeout = 60) for x in data['ghana_city']]
# Extract lat/long to separate columns
data['g_latitude'] = data['geocode_result'].apply(lambda loc: loc.latitude)
data['g_longitude'] = data['geocode_result'].apply(lambda loc: loc.longitude)
Result
(由于缺乏声誉,我无法发表评论,所以我在这里回答坐标下降混乱)。
str(location)
returns 一个文本地址(没有坐标),但是 repr(location)
returns 以下格式的字符串(包括坐标):
Location(%(address)s, (%(latitude)s, %(longitude)s, %(altitude)s))
打印 data
时看到的内容使用 repr
(pandas 似乎为了简洁而删除了前导 Location
类型),因此您可以看到坐标。但是当列转换为 str
时,它使用 str
表示,不包括坐标。这就是全部的魔力。