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
CodePudding user response:
Something like this:
df['distance'] = df.apply(lambda r: distance(r[0], r[1], r[2], r[3], r[4], r[5]), axis=1)