Compute Euclidean distance between rows of two pandas dataframes

前端 未结 1 1429
执念已碎
执念已碎 2020-12-17 21:32

I have two pandas dataframes d1 and d2 that look like these:

d1 looks like:

  output   value1   value2   value         


        
相关标签:
1条回答
  • 2020-12-17 22:06

    By using scipy.spatial.distance.cdist:

    import scipy
    
    ary = scipy.spatial.distance.cdist(d1.iloc[:,1:], d2.iloc[:,1:], metric='euclidean')
    
    pd.DataFrame(ary)
    Out[1274]: 
                0           1          2           3           4          5
    0    0.000000  101.167485  65.886266    0.000000  101.167485  65.886266
    1  101.167485    0.000000  71.808495  101.167485    0.000000  71.808495
    2   65.886266   71.808495   0.000000   65.886266   71.808495   0.000000
    
    0 讨论(0)
提交回复
热议问题