Python 将带有地址和坐标的列转换为带有.astype(str)的字符串会删除坐标

Python 将带有地址和坐标的列转换为带有.astype(str)的字符串会删除坐标,python,regex,string,geocoding,geopy,Python,Regex,String,Geocoding,Geopy,我正在使用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.74

我正在使用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']]

我只想从这里得到坐标

使用extract似乎不起作用,只返回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

有人能帮忙吗?非常感谢


虚拟数据:

我要从中提取坐标的列是geocode\u result2或geocode\u 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
Location(%(address)s, (%(latitude)s, %(longitude)s, %(altitude)s))

您可以使用
.apply
.str

Ex:

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
输出:

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
返回
Location
对象而不是字符串(尽管其字符串表示形式实际上包含您试图解析的lat/long),因此可以通过分别访问
Location.latitude
/
Location.longitude
属性来检索lat/long

# 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)

(由于声誉不佳,我无法发表评论,所以我在这里回答这个问题)

str(位置)
返回文本地址(无坐标),但
repr(位置)
返回以下格式的字符串(包括坐标):


打印
数据
时看到的内容使用
repr
(为了简洁起见,熊猫似乎会删除前导的
位置
类型),因此您可以看到坐标。但是当该列转换为
str
时,它使用
str
表示,其中不包括坐标。这就是这里的全部魔力。

试试
p=r'(?p-?\d+\.\d+)\s*(?p-?\d+\.\d+)”
这两个值之间有逗号和空格字符,对吗?谢谢你的帮助,我以前确实试过。还尝试使正则表达式变得非常简单。我有一种感觉,就是.str不能正常工作,虽然当我在列上键入数据时,它返回的“object”不是可疑的,但我似乎无法在不删除坐标的情况下转换这个该死的东西,这是我认为我需要做的提取工作感谢Rakesh,这实际上是抛出了错误“没有足够的值来解包(预期为2,得到0)”-这似乎没有意义,因为数据帧中至少前几行包含floatsCan,您可以发布数据['geocode_result2']这样我就可以测试了。它与我的虚拟数据一起工作。我已经编辑了我的答案,当我从'data['g_lation'],data['g_longitude']=data['geocode_result2].apply(lambda x:getLatLog(x))中删除.str时,它包含了geocode_result2有趣的3行.str“我有;太多的值无法解压您是否认为我拥有的列可能来自geopy软件包的响应,而不是我在列中输入字符串,这导致了问题?感谢您的回答,我使用您的代码“NoneType”对象没有属性“latitude”时出错“必须先删除没有行的行。试试
data=data.dropna(axis=0)
就这样-感谢克斯特亚所做的一切