简体   繁体   中英

Is there a simple way to calculate the distance of two points in 3D space for a whole Data Set in python?

I am new to python and I want to quickly and dirty create a table of two coordinates(their x,y,z value) and their distances to each other.

The data set looks like this

data = np.random.randint(5,30,size=(10,6))
df = pd.DataFrame(data, columns=['x1', 'y1', 'z1', 'x2', 'y2', 'z2'])

print(df)

To a distance column, I want to apply an easy function like

def distance(x1, y1, z1, x2, y2, z2):
    
    d = math.sqrt(math.pow(x2 - x1, 2) +
                math.pow(y2 - y1, 2) +
                math.pow(z2 - z1, 2)* 1.0)
    return d 

I also tried to use arrays (a and b) as inputs but I don't understand yet how I can have an array as a variable in a function like

p1 = a
p2 = b

squared_dist = np.sum((p1-p2)**2, axis=0)
dist_ab = np.sqrt(squared_dist)
print(dist_ab)

How would you guys approach this task? I hope this is easy to understand because I think I got lost and went crazy in the last hours :D

像这样的东西:

df['distance'] = df.apply(lambda r: distance(r[0], r[1], r[2], r[3], r[4], r[5]), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM