api.foursquare.com/v2/venues/在python数据框中搜索每个坐标

时间:2018-11-05 14:43:43

标签: python-3.x foursquare

结果数据帧

Municipality    Latitude    Longitude   Population2011

0 Agia Paraskevi 38.01263 23.82055 59,704 1 Agios Dimitrios 37.93667 23.73320 71,294 2 Alimos 37.91368 23.71506 41,720

search_query = 'Hospital'
categoryId = '4bf58dd8d48988d104941735'

定义函数以遍历数据帧的每一行并使用其坐标

为此坐标获取所有“医院”场所

    def getNearbyVenues(names, latitudes, longitudes, radius=500):

    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):


        # create the API request URL
        url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}&locale={}&categoryId={}'.format(CLIENT_ID, CLIENT_SECRET, latitudes, longitudes, VERSION, search_query, radius, LIMIT, locale, categoryId)


        # make the GET request
        results = requests.get(url).json()["response"]

        # return only relevant information for each nearby venue
        venues_list.append([(
            name, 
            lat, 
            lng, 
            v['venues']['name'], 
            v['venues']['location']['lat'], 
            v['venues']['location']['lng']) for v in results])

    nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])

    return(nearby_venues)

使用数据框的“结果”列作为输入来调用上述函数:

`all_venues = getNearbyVenues(names=result['Municipality'],
                                   latitudes=result['Latitude'],
                                   longitudes=result['Longitude']
                                  )`                                  )

你好! 拜托我需要你的帮忙! 我尝试将每个数据框的坐标设置为输入,以获取每个市镇的医院并创建一个新的数据框。

但是似乎我无法正确解析json(空字典...)

1 个答案:

答案 0 :(得分:0)

你好!

我对功能进行了少许修改并起作用:

def getNearbyVenues(names, lat1, long1, radius=3000):

venues_list=[]
for name, lat, lng in zip(names, lat1, long1):

    # create the API request URL
    url1 = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}&locale={}&categoryId={}'.format(CLIENT_ID, CLIENT_SECRET, lat, lng, VERSION, search_query, radius, LIMIT, locale, categoryId)


    # make the GET request
    results = requests.get(url1).json()["response"]["venues"]

    # return only relevant information for each nearby venue
    venues_list.append([(
        name, 
        lat, 
        lng, 
        v['name'], 
        v['location']['lat'], 
        v['location']['lng']) for v in results])

    nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])

return(nearby_venues)