# Many to many distance matrix in python

**URL:** https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957
**Category:** python
**Created:** [June 9, 2022, 10:58pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957 "2022-06-09T22:58:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![barbosasilva.roberta](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/barbosasilva.roberta/32/1735_2.png) [@barbosasilva.roberta](https://ask.openrouteservice.org/u/barbosasilva.roberta)
#### Post date: [June 9, 2022, 10:58pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/1 "2022-06-09T22:58:01Z")

</div>

Hi,  
I’d like to know how i can make a many-to-many query to calculate de distance in km and driving car option for diffent locations of origin and destination. I have a dataframe in which one column has the latitude and longitude for the origins and the other column has the latitude and longitude for the destinations.

I’ve tried this way, but I didn’t succeed

```auto
import openrouteservice

df_result = pd.DataFrame()
client = openrouteservice.Client(key='using_my_key) 
row = 0
for i in range(df.shape[0]):
    city = i
    coords = (city['origin'][i],city['destination'][i])
    routes = client.directions(coords)
    df_result = [city['nota_id'][i], city['origin'][i],city['destination'][i],routes['routes'][0]['summary']['distance']]
    time.sleep(3)
    row = row + 1

```

Thank you!

---

<div class="post-metadata">

### Author: ![jschnell](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/jschnell/32/971_2.png) [@jschnell](https://ask.openrouteservice.org/u/jschnell)
#### Post date: [June 10, 2022, 8:06am UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/2 "2022-06-10T08:06:31Z")

</div>

Hey,

you can use the [matrix endpoint](https://openrouteservice.org/dev/#/api-docs/v2/matrix/%7Bprofile%7D/post) for doing so. Note the [limits](https://openrouteservice.org/plans/) of the free API token and the [restrictions](https://openrouteservice.org/restrictions/) for the matrix endpoint when doing so.

You can find an example in python [here](https://github.com/GIScience/openrouteservice-py/blob/master/examples/basic_example.ipynb). As for how this interfaces correctly with pandas `DataFrame`, see any pandas tutorial. Note, that the ors expects coordinates in [Lon, Lat]-Format.

Best regards

---

<div class="post-metadata">

### Author: ![barbosasilva.roberta](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/barbosasilva.roberta/32/1735_2.png) [@barbosasilva.roberta](https://ask.openrouteservice.org/u/barbosasilva.roberta)
#### Post date: [June 22, 2022, 5:34pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/3 "2022-06-22T17:34:33Z")

</div>

Hi!! Thanks for your answer.  
I managed to get the query with the following code, in case anyone else is with the same doubt

Here is my code:

```auto
client = openrouteservice.Client(key='my_key') # Specify your personal API key
columns_names = ['nota_id', 'long_ced','lat_ced', 'long_sac','lat_sac', 'Distance']
test_out = pd.DataFrame(columns=columns_names, dtype=object) 
row = 0
for i in test_input['nota_id']:
    try:
        nota_id = i
        coords = ((test_input.loc[row,'long_ced'],test_input.loc[row,'lat_ced']),(test_input.loc[row,'long_sac'],test_input.loc[row,'lat_sac']))
        routes = client.directions(coords, profile='driving-car', units='km')
        test_out.loc[row] = [i, test_input.loc[row,'long_ced'],test_input.loc[row,'lat_ced'],test_input.loc[row,'long_sac'],test_input.loc[row,'lat_sac'], routes['routes'][0]['summary']['distance']]
        time.sleep(3)
        row = row + 1
    except:
        time.sleep(3)
        row = row + 1
        continue
   

```

Thanks for the help!!

---

<div class="post-metadata">

### Author: ![jschnell](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/jschnell/32/971_2.png) [@jschnell](https://ask.openrouteservice.org/u/jschnell)
#### Post date: [June 23, 2022, 6:46am UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/4 "2022-06-23T06:46:42Z")

</div>

Hey,

this is still using the directions endpoint - since you only need the distances, I’d strongly recommend you switch to the matrix endpoint.

I am no expert in using pandas DataFrames, but after looking at [this thread on the forum](https://ask.openrouteservice.org/t/pandas-dataframe/3104/5) and [the docs](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html), something like `DF.loc([['long_ced', 'lat_ced']]).values().tolist()` might work to get a list of starting positions as ors matrix would expect it.

Best regards

---

<div class="post-metadata">

### Author: ![aivindot](https://ask.openrouteservice.org/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@aivindot](https://ask.openrouteservice.org/u/aivindot)
#### Post date: [July 26, 2022, 3:53pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/5 "2022-07-26T15:53:13Z")

</div>

Any idea to go about the same many to many distance matrix computation without the restrictions? I am dealing with about 100 by 100 square matrix. The free API gives me limit error.

---

<div class="post-metadata">

### Author: ![amandus](https://ask.openrouteservice.org/user_avatar/ask.openrouteservice.org/amandus/32/669_2.png) [@amandus](https://ask.openrouteservice.org/u/amandus)
#### Post date: [July 26, 2022, 4:00pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/6 "2022-07-26T16:00:43Z")

</div>

Hey @aivindot,

you can split your data set to pass a maximum of 3500 pairs with each request.  
e.g. 4 requests with 25x100 matrix.

Best regards

---

<div class="post-metadata">

### Author: ![aivindot](https://ask.openrouteservice.org/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@aivindot](https://ask.openrouteservice.org/u/aivindot)
#### Post date: [July 27, 2022, 1:34pm UTC](https://ask.openrouteservice.org/t/many-to-many-distance-matrix-in-python/3957/7 "2022-07-27T13:34:37Z")

</div>

@amandus, Thanks. I will try that out.
