Traversing Data
Explore how to efficiently calculate distances between all locations by automating the process with loops and dataframes in Python. Understand the logic behind avoiding redundant calculations, exporting results as CSV files, and preparing data crucial for solving the Traveling Salesperson Problem.
We'll cover the following...
We calculated the distances between two places. However, we need to calculate the distances between all locations to be able to calculate the shortest total distance. Since we don’t want to repeat the previous step manually for all locations, we’ll solve it via a loop instead.
Distance between all locations
To get started, let’s first calculate the distance between three stores: A, B, and C. With islice, we iterate over the df.iterrows() iterable, which starts at counterFixed. Using df.index != i, we ensure that the distance from any store to itself is not calculated.
Iterate and calculate
The loop grabs the first three stores and calculates the distances from one store to each of the two other stores.
The result will be packed into a DataFrame with the From, To, Duration(s), and Distance(m) column headers. At this point, we have the index numbers of these stores. To also receive the store names (like StoreA, etc.), we can combine this ...