shawn
shawn

Reputation: 13

Add calculated column to df2 for every row entry in df2 pandas dataframe

I have 2 dataframes

df1 = pd.DataFrame({'X':[1,2,3,4,5],'Y':[1,2,3,4,5],'Point':[1,2,3,4,5]})
X Y Point
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
df2 = pd.DataFrame({'X':[1,2,3,4,5,6,7,8],'Y':[1,2,3,4,5,6,7,8]})
X Y
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8

I need to add a new column into df2 for every row entry in df1 and populate with the following equation

=sqrt((abs(df1.X-df2.X)**2)+(abs(df1.Y-df2.Y)**2))

Each new column title should correspond to the Point column entry in df1 whats the best way to do this

should look like this

X Y Point1 Point2 Point3 Point4 Point5
1 1 0 1.41 2.83 4.24 5.66
2 2 1.41 0 1.41 2.83 4.24
3 3 2.83 1.41 0 1.41 2.83
4 4 4.24 2.83 1.41 0 1.41
5 5 5.66 4.24 2.83 1.41 0
6 6 7.07 5.66 4.24 2.83 1.41
7 7 8.49 7.07 5.66 4.24 2.83
8 8 9.90 8.49 7.07 5.66 4.24

Upvotes: 1

Views: 42

Answers (1)

not_speshal
not_speshal

Reputation: 23166

One possibility is to use the cdist function from scipy.

from scipy.spatial.distance import cdist
output = pd.DataFrame(data=cdist(df1[["X", "Y"]], df2, 'euclidean'), 
                      index=pd.MultiIndex.from_frame(df2[["X","Y"]]),
                      columns=[f"Point{i+1}" for i in range(5)]).reset_index()

>>> output
   X  Y    Point1    Point2    Point3    Point4    Point5
0  1  1  0.000000  1.414214  2.828427  4.242641  5.656854
1  2  2  1.414214  0.000000  1.414214  2.828427  4.242641
2  3  3  2.828427  1.414214  0.000000  1.414214  2.828427
3  4  4  4.242641  2.828427  1.414214  0.000000  1.414214
4  5  5  5.656854  4.242641  2.828427  1.414214  0.000000
5  6  6  7.071068  5.656854  4.242641  2.828427  1.414214
6  7  7  8.485281  7.071068  5.656854  4.242641  2.828427
7  8  8  9.899495  8.485281  7.071068  5.656854  4.242641

Upvotes: 0

Related Questions