使用Geocoder查找非地址点,返回单个组件,然后附加到当前数据框?

问题描述 投票:0回答:1

我目前正在开展一个项目,该项目着眼于纽约市各行政区的标准化测试范围。我有一个数据集,提供区域自治市镇号码(DBN),但没有提供地址。当我在谷歌搜索DBN时,它确实提供了每所高中的确切地址。

我是熊猫新手所以请耐心等待。我试图让地理编码器遍历区域自治区号码列,然后将输出附加到当前数据帧。


import pandas as pd  
import geocoder
from googlegeocoder import GoogleGeocoder

url="https://raw.githubusercontent.com/linnaha/NYCdata/master/sat_2014.csv"
            satdata = pd.read_csv(url, encoding='latin-1')
            satdata.head()

image of sample data


geocoder = GoogleGeocoder()
list_of_dbn = satdata['DBN']


for address in list_of_dbn:
    try:
        search = geocoder.get(address)
    except ValueError:
        continue
    first_result = search[0]
    output =first_result.formatted_address
    print(output)
    satdata["zip_code"]= output

它遍历列表并返回地址


220 Henry St, New York, NY 10002, USA
200 Monroe St, New York, NY 10002, USA
420 E 12th St, New York, NY 10009, USA
198 Forsyth St, New York, NY 10002, USA
145 Stanton St, New York, NY 10002, USA
145 Stanton St, New York, NY 10002, USA
111 Columbia St, New York, NY 10002, USA
198 Forsyth St, New York, NY 10002, USA
525 E Houston St, New York, NY 10002, USA
225 E 23rd St, New York, NY 10010, USA
525 W 50th St, New York, NY 10019, USA
350 Grand St, New York, NY 10002, USA

但是当我再次查看数据集时,它只会为所有行重复相同的地址。 see here


最后,如果它可以工作,我怎么能将字符串拆分为只有邮政编码和城市?这就是我尝试过的。我也尝试过使用Nominatim,但它不承认DBN。


new_list = []
for var in satdata.zip_code:
    new_list.append(var.rsplit(maxsplit = 1)[0].replace(" ","_"))
satdata.zip_code = new_list

145_Stanton_St,_New_York,_NY_10002,

python pandas geocoding
1个回答
0
投票

在你的for循环的每次迭代中,你用"zip_code"覆盖整个output

satdata["zip_code"]= output

因此,很容易解释所有行中具有相同值的输出。你可以初始化:

satdata["zip_code"]=''

然后呢

satdata.loc[satdata['DBN']==adress,"zip_code"]= output

在for循环中。如果有效,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.