首页 > 解决方案 > 有没有办法使用 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 获得自己的访问令牌

标签: pythonjsonasynchronoushttprequest

解决方案


推荐阅读