python - 在 pandas 数据帧上使用循环进行 Foursquare 调用
问题描述
卡在项目的一个步骤上。对于这一部分,我有一个数据框 (df_income_zip_good),其中包含美国人均收入排名前 100 的城镇的信息。
数据框如下所示:
我已经用 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 中的纬度、经度的每个组合刮取信息。
# 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 循环。
不知道去哪里,任何指导将不胜感激。
谢谢!
解决方案
如果你看你的代码
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)
最后几行不是循环的一部分(它们不像其他行那样缩进)。所以首先执行循环,然后在循环中最后设置的值上执行其他行。
推荐阅读
- c# - MailSystem.Net 有没有办法检查电子邮件的状态(是已读还是未读)?
- c# - 从winform中的子用户控件调用位于父用户控件中的函数
- optimizely - 优化账号注册
- reactjs - useEffect Hook 不会触发包含在其中的函数
- qt - 查找已连接的 Wifi 网络
- python - 如何推断简单的线性回归并获得 Python 中系数的错误?
- android - 用一个按钮打开文件选择器,用第二个按钮打开相机应用程序 android webview kotlin
- jquery - 尝试通过 post() 方法使用 onclick 函数从标签传递数据
- ios - iOS-13 - UITableViewCell 的 CornerRadius
- c# - 即使中间件返回 AuthenticateResult.Success 也未授权