2

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
Abdulaziz Al Jumaia
  • 436
  • 2
  • 5
  • 22

1 Answers1

2

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.

Rob Bricheno
  • 4,467
  • 15
  • 29
  • I encountered `ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()`. at the first time. So, I had to use `all()`. Anyway, after I followed your instruction, I received this error `ValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.` – Abdulaziz Al Jumaia Nov 20 '18 at 22:54
  • 1
    @AbdulazizAlJumaia I suspect your file contains blank lines. Try removing those lines. Or paste the csv file you are using into the question, I had to construct a fake one for testing, but I am happy to test with real data. – Rob Bricheno Nov 20 '18 at 22:57
  • I have more than 11000 instances. But, I put 20 instances above. I did check out the data, they are fine. Only the second row is blank which is removed in the slices I put in the code. Could this be a problem from measuring the distance of the same location? @Rob Bricheno – Abdulaziz Al Jumaia Nov 20 '18 at 23:06
  • 1
    @AbdulazizAlJumaia Please post the sample exactly as it appears in the CSV file, including commas, quotes, etc. I'm trying to reproduce your problem, which I can't do with data I have made up, so I need a real example of data that causes this issue. If I edit your data like this for example there is no issue https://pastebin.com/N7GpmeS8 – Rob Bricheno Nov 20 '18 at 23:10
  • Here is the sample https://pastebin.com/KKqz2rSH or you can go back to the question. I have edited it. I just noticed that there are blanks in the data. @Rob Bricheno – Abdulaziz Al Jumaia Nov 20 '18 at 23:25
  • 1
    @AbdulazizAlJumaia Thanks! It is definitely those blanks that are causing the problem. You can either edit them out, or hide the problem by wrapping the `print` line inside a `try: ... except ValueError: ...` block. – Rob Bricheno Nov 20 '18 at 23:29