-1

Panda Excel File Reading

I am attempting to try and access an excel files rows and columns and do some basic distance calculations, but I keep getting the same values> i am attempting to find the distance from any two points of the map by iterating through the list of edges and their corresponding values in the dictionary I created. not sure what I am doing wrong here. Any guidance is appreciated.

import pandas as pd
import array
import numpy as np
import pyomo.environ as pyo
from math import sin, cos, sqrt, atan2, radians
import requests
import urllib.request
import time
from bs4 import BeautifulSoup

distance = pd.read_csv("Distance File.csv")
distance.head()

d_long = distance["Longitude"]
d_lat = distance["Latitude"]
print(d_long)
print(d_lat)

distance_dict = {}
location = list(range(0, 10))
print(location)
for i in d_long:
    for j in d_lat:
        for x in location:
            distance_dict[x] = i, j
            # distance_dict.update(d1)
print(distance_dict)

opt = pyo.SolverFactory("glpk")
V = list(distance_dict.keys())  # List of Customers
Customers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
distance_dict = distance_dict  # Dict of customers

E = []

for i in V:
    for j in V:
        if i != j:
            E.append((i, j))
print(E)
c = {}
for (i, j) in E:
    lat = radians(abs(distance_dict[i][1]))
    lon = radians(abs(distance_dict[i][0]))
    lat2 = radians(abs(distance_dict[j][1]))
    lon2 = radians(abs(distance_dict[j][0]))
    dlon = lon2 - lon
    dlat = lat2 - lat
    a = sin(dlat / 2) ** 2 + cos(lat) * cos(lat) * sin(dlon / 2) ** 2
    c[i, j] = 2 * atan2(sqrt(a), sqrt(1 - a))
    print(dlat)
print(c)

1 Answers1

0

you're getting the same distance every time because of the way you're structuring your dictionary. specifically, this code right here:

for i in d_long:
    for j in d_lat:
        for x in location:
            distance_dict[x] = i, j

for all the keys in location, you set the value last values of whatever's in d_lat/long, overwriting what was put in there on any previous iteration.

to test, try printing your dictionary after the location for loop.

acushner
  • 9,595
  • 1
  • 34
  • 34