0

I have this code

import numpy as np
import pandas as pd
from scipy.spatial import distance
mn= [(252, 468), (252, 495), (274, 481), (280, 458), (298, 479), (301, 458), (324, 499)]
name=['loc1','loc2','loc3','loc4','loc5','lco6','loc7']

zz= [(329, 478), (336, 455), (346, 499), (352, 478), (374, 468), (381, 499), (395, 459), (406, 488)]

L = pd.Series()
for name, i in list(zip(name, mn)):
    for e in zz:
        L[name] = distance.euclidean(e, i)
print(L)

w=100
dd = np.sqrt(np.power(L, 2) + np.power(w, 2))
print(dd)

Here is what it gives as an output for L & dd:

loc1    155.293271
loc2    154.159009
loc3    132.185476
loc4    129.522199
loc5    108.374351
lco6    109.201648
loc7     82.734515
dtype: float64
loc1    184.705170
loc2    183.752551
loc3    165.749811
loc4    163.633737
loc5    147.461859
lco6    148.070929
loc7    129.788289

my trouble that it only gives L&dd for one point in zz, but what I want to have L for each point in zz and then be able to use it to get dd for each value in L.

Thanks for your help!

1 Answer 1

2

You are almost there. Basically you can replace your for loop with this:

L = pd.Series()
for name, i in list(zip(name, mn)):
    j = [] # save the intermediary results here
    for e in zz:
        j.append(distance.euclidean(e, i)) 
    L[name] = j # append at once all computations are done

This will give you something like this:

loc1    [77.64663547121665, 85.0, 98.97979591815695, 1...
loc2    [78.85429601486528, 93.03762679690406, 94.0850...
loc3    [55.08175741568164, 67.23094525588644, 74.2159...
loc4    [52.92447448959697, 56.08029957123981, 77.6981...
loc5    [31.016124838541646, 44.94441010848846, 52.0, ...
lco6    [34.40930106817051, 35.12833614050059, 60.8769...
loc7    [21.587033144922902, 45.60701700396552, 22.0, ...

Next step, you can do make use of .apply functions:

op = (L
     .apply(lambda x: np.sqrt(np.power(x, 2) + np.power(w, 2)))
     .apply(lambda x: x[0])) # get the first value of each array

loc1    126.605687
loc2    127.349912
loc3    114.166545
loc4    113.141504
loc5    104.699570
lco6    105.754433
loc7    102.303470
dtype: float64
Sign up to request clarification or add additional context in comments.

2 Comments

THANKS :) and for the next operation, I did it like that o = [] for row in L: o.append(np.sqrt(np.power(row, 2) + np.power(w, 2))) dd = o it gives [array([126.60568708, 131.24404748, ........]), array([127.34991166, 136.58696863, ....... ]) So, is there a way to call the first columns of dd? Thanks!
I did but I am still a new member, I should reach 15 to be able to mark it. they register my marking until I reach 15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.