Abdulaziz Al Jumaia
Abdulaziz Al Jumaia

Reputation: 436

Can't loop the distance between two locations. TypeError: 'numpy.float64' object is not iterable

I have a csv file with locations: enter image description here

I have written a program that measures the distance between two locations. As I have many locations, I created a loop to iterate over the locations above.

import pandas as pd
import numpy as np
from pandas import DataFrame

Data = pd.read_csv('/home/aziz/Desktop/langlat.csv')
data = pd.DataFrame(Data)
lat1 = data['Lattude'][2:]
lat = pd.DataFrame(np.array(lat1))
lang1 = data['Langitude'][2:]
lang = pd.DataFrame(np.array(lang1))

import geopy.distance


for i in range(len(lat)):
    for j in range(len(lat)):
        coords_1 = (all(lat[0][i]), all(lang[0][i]))
        coords_2 = (all(lat[0][j]), all(lang[0][j]))
        print(geopy.distance.distance(coords_1, coords_2).km)

Yet, the output is:

TypeError: 'numpy.float64' object is not iterable

if I use this code, it will return the distance as desired.

coords_1 = (lat[0][3], lang[0][3])
coords_2 = (lat[0][5], lang[0][5])
print(geopy.distance.distance(coords_1, coords_2).km)

Output

84.44162834864254

From a little research, I knew that my data is 1-D. But, I could not figure out a way to solve the problem. So, how can I make the program iterate over new locations?

part of the data:

Lattude,Langitude
,
26.332805,44.80257
24.849348,46.823551
,
24.848709,46.814429
24.585251,46.807482

Upvotes: 2

Views: 413

Answers (1)

Rob Bricheno
Rob Bricheno

Reputation: 4653

The full traceback shows us exactly what is causing that error.

Traceback (most recent call last):
  File "/home/rob/test/test.py", line 17, in <module>
    coords_1 = (all(lat[0][i]), all(lang[0][i]))
TypeError: 'numpy.float64' object is not iterable

Lose those alls and it works:

for i in range(len(lat)):
    for j in range(len(lat)):
        coords_1 = (lat[0][i], lang[0][i])
        coords_2 = (lat[0][j], lang[0][j])
        print(geopy.distance.distance(coords_1, coords_2).km)

lat[0][i] for example is a single floating point number, and all expects an iterable type. I don't understand what you were trying to do with all.

Upvotes: 2

Related Questions