python - 有没有办法使用 pandas 数据帧快速发出 270K API 请求?
问题描述
我有以下数据集(这只是 270K 中的 5 个观察值):
ride_id_hash start_lat start_lon end_lat end_lon
0 3913897986482697189 52.514532 13.389043 52.513640 13.380389
1 -742299897365889600 52.515073 13.394545 52.518483 13.407965
2 -319522908151650562 52.528291 13.409570 52.526965 13.415183
3 -3247332391891858034 52.514276 13.453460 52.503649 13.448538
4 5368542961279891088 52.504448 13.468405 52.511891 13.474998
我想向 API 发出请求,并根据 Mapbox 的经纬度起点-目的地检索估计的距离和时间。因为我必须使用 270 行,所以这个过程非常缓慢。我编写了以下代码:
acces_token = 'pk.eyXXXX'
def create_route_json(row):
"""Get route JSON."""
base_url = 'https://api.mapbox.com/directions/v5/mapbox/cycling/'
url = base_url + str(row['Start Lon']) + \
',' + str(row['Start Lat']) + \
';' + str(row['End Lon']) + \
',' + str(row['End Lat'])
print('start lon: ' + str(row['Start Lon']))
print('start lat: ' + str(row['Start Lat']))
print('end lon: ' + str(row['End Lon']))
print('end lat: ' + str(row['End Lat']))
params = {
'geometries': 'geojson',
'access_token': acces_token
}
req = requests.get(url, params=params)
route_json = req.json()['routes'][0]
print('Duration: ' + str(route_json['duration']))
print('Distance: ' + str(route_json['distance']))
print('^'*40)
row['Estimated_duration'] = route_json['duration']
row['Estimated_distance'] = route_json['distance']
#mystr= str(route_json['duration']) + ';'+ str(route_json['distance'])
return row
start = time.time()
df_process.apply(create_route_json, axis=1)
我想知道是否有办法快速拨打 270K 电话。(您需要从 Mapbox Directions API 获得自己的访问令牌
解决方案
推荐阅读
- html - 在 HTML 的 vs 代码中禁用波浪黄色下划线
- python - 将图像转换为范围为 [0,255] 而不是 [0,1] 的张量?
- python - Conway 的生命游戏 Python / PyGame 打印错误
- python - 如何跟踪单个棋子的移动?
- javascript - 在服务器端发生帖子后,如何使用客户端 JavaScript 显示警报?
- android - 无障碍服务元数据文件出现问题 [Xamarin Android]
- c - 查找矩阵 NxN 中的所有峰值
- c# - 更改游戏对象,当速度计的角度发生变化时
- javascript - 用于动态更改内容的引导工具提示
- reactjs - 为什么在 React 中获取用户的地理位置时出现无限循环?