首页 > 解决方案 > 在 pandas 数据帧上使用循环进行 Foursquare 调用

问题描述

卡在项目的一个步骤上。对于这一部分,我有一个数据框 (df_income_zip_good),其中包含美国人均收入排名前 100 的城镇的信息。

数据框如下所示:

df_income_zip_good.head()

我已经用 folium 在这个数据框上运行了一个循环,并且能够毫无问题地进行映射。

for i, series in df_income_zip_good.iterrows():
    lat = series ['lat']
    lng = series ['lng']
    town = series ['place']

    folium.Marker (location=[lat,lng], popup = town, icon = folium.Icon(color='blue')).add_to(map_usa)
map_usa

来自 df_income_zip_good 的叶图绘制位置

我没有成功地尝试编写另一个循环,该循环将从四方中为 df_income_zip_good 中的纬度、经度的每个组合刮取信息。

 # scraping the foursquare website for the information we want and obtaining the json file as results

for i, series in df_income_zip_good.iterrows():
    lat = series ['lat']
    lng = series ['lng']
    town = series ['place']
    LIMIT = 100
    radius = 1000
    url4Sqr = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
        CLIENT_ID,
        CLIENT_SECRET,
        VERSION,
        lat,
        lng,
        radius,
        LIMIT)

#export results to json file
result4Sqr = requests.get(url4Sqr).json()["response"]['groups'][0]['items']


#print results from call
print (result4Sqr)

请求的 json 打印出来,但它只打印 df_income_zip_good 中的一行。它似乎没有在整个 df_income_zip_good 循环。

不知道去哪里,任何指导将不胜感激。

谢谢!

标签: pythonpandasapiloopsfoursquare

解决方案


如果你看你的代码

or i, series in df_income_zip_good.iterrows():
    ...

#export results to json file
result4Sqr = requests.get(url4Sqr).json()["response"]['groups'][0]['items']


#print results from call
print (result4Sqr)

最后几行不是循环的一部分(它们不像其他行那样缩进)。所以首先执行循环,然后在循环中最后设置的值上执行其他行。


推荐阅读