Can't loop the distance between two locations. TypeError: 'numpy.float64' object is not iterable
I have a csv file with locations:
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
python python-3.x python-3.6 geopy
add a comment |
I have a csv file with locations:
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
python python-3.x python-3.6 geopy
add a comment |
I have a csv file with locations:
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
python python-3.x python-3.6 geopy
I have a csv file with locations:
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
python python-3.x python-3.6 geopy
python python-3.x python-3.6 geopy
edited Nov 21 at 12:04
asked Nov 20 at 22:24
Abdulaziz Al Jumaia
118113
118113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 all
s 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
.
I encounteredValueError: 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 useall()
. Anyway, after I followed your instruction, I received this errorValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.
– Abdulaziz Al Jumaia
Nov 20 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 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 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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
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 theprint
line inside atry: ... except ValueError: ...
block.
– Rob Bricheno
Nov 20 at 23:29
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402504%2fcant-loop-the-distance-between-two-locations-typeerror-numpy-float64-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 all
s 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
.
I encounteredValueError: 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 useall()
. Anyway, after I followed your instruction, I received this errorValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.
– Abdulaziz Al Jumaia
Nov 20 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 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 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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
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 theprint
line inside atry: ... except ValueError: ...
block.
– Rob Bricheno
Nov 20 at 23:29
|
show 1 more comment
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 all
s 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
.
I encounteredValueError: 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 useall()
. Anyway, after I followed your instruction, I received this errorValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.
– Abdulaziz Al Jumaia
Nov 20 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 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 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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
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 theprint
line inside atry: ... except ValueError: ...
block.
– Rob Bricheno
Nov 20 at 23:29
|
show 1 more comment
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 all
s 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
.
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 all
s 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
.
edited Nov 20 at 22:41
answered Nov 20 at 22:36
Rob Bricheno
2,315218
2,315218
I encounteredValueError: 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 useall()
. Anyway, after I followed your instruction, I received this errorValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.
– Abdulaziz Al Jumaia
Nov 20 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 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 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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
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 theprint
line inside atry: ... except ValueError: ...
block.
– Rob Bricheno
Nov 20 at 23:29
|
show 1 more comment
I encounteredValueError: 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 useall()
. Anyway, after I followed your instruction, I received this errorValueError: Point coordinates must be finite. (nan, nan, 0.0) has been passed as coordinates.
– Abdulaziz Al Jumaia
Nov 20 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 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 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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
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 theprint
line inside atry: ... except ValueError: ...
block.
– Rob Bricheno
Nov 20 at 23: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 at 22:54
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 at 22:54
1
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 at 22:57
@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 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 at 23:06
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 at 23:06
1
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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
@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 pastebin.com/N7GpmeS8
– Rob Bricheno
Nov 20 at 23:10
1
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 at 23:29
@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 at 23:29
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402504%2fcant-loop-the-distance-between-two-locations-typeerror-numpy-float64-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown