1

We have two points in Cartesian space as origins and some other typical points. For typical points we are only interested in their distance from the origins: two numbers. so we want reach from df

d = {'origin1': [(1,0,0), (0,0,0)],
     'origin2': [(2,0,0), (0,0,1)],
     'point1': [(40,0,0), (0,0,20)],
     'point2': [(50,0,0), (0,0,25)],
     'point3': [(60,0,0), (0,0,30)]}

display(pd.DataFrame(data=d, index=[0, 1]))

to df

d = {'origin1': [(1,0,0), (0,0,0)],
     'origin2': [(2,0,0), (0,0,1)],
     'point1': [(39,38), (20,19)],
     'point2': [(49,48), (25,24)],
     'point3': [(59,58), (30,29)]}

display(pd.DataFrame(data=d, index=[0, 1]))

Of course here we chose simple number for simple distance to see the problem. in general case we should use Pythagorean distance formula.

moshtaba
  • 381
  • 1
  • 8

1 Answers1

1

It's a solution for arbitrary dimensions of points & number of origins that we may have:

d = {'origin1': [(1,0,0) , (0,0,0 )],
     'origin2': [(2,0,0) , (0,0,1 )],
     'point1' : [(40,0,0), (0,0,20)],
     'point2' : [(50,0,0), (0,0,25)],
     'point3' : [(60,0,0), (0,0,30)]}

df_pnt = pd.DataFrame(data=d, index=[0, 1])
df_pnt

First we define some functions:

import pandas as pd

def distance(p1, p2):
    '''
    Calculate the distance between two given points
    Arguments:
        p1, p2: points
    Returns:
        Distance of points
    '''
    dmn = min(len(p1), len(p2))
    vct = ((p1[i] - p2[i])**2 for i in range(dmn))
    smm = sum(vct)
    dst = smm**.5
    return dst

def distances(df, n):
    '''
    Calculate the distances between points & origins
    Arguments:
        df: dataframe of points including origins
        n : number of origins
    Returns:
        dataframe of distances
    '''
    df_dst = df.iloc[:, :n]
    for column in df.columns[n:]:
        df_dst[column] = df.apply(lambda row: tuple(distance(row[origin], row[column]) for origin in df.columns[:n]), axis=1)
    return df_dst

Now this script gives your desired output:

distances(df_pnt, 2)

I hope it be what you want.

Ali Ashja'
  • 168
  • 2
  • 8
  • حاجی دمت گرم کشتۀ آربیتری دایمنشن شدم. ولی خیلی کنده. ایده ای داری سریعتر بشه؟ – moshtaba May 23 '22 at 16:48
  • 1
    For timing & precision optimization, you can replace my 'distance' function with any of prepared python functions: https://stackoverflow.com/questions/37794849/efficient-and-precise-calculation-of-the-euclidean-distance, of course, if you have no worry about importing more packages. – Ali Ashja' May 24 '22 at 08:40