Distanz-Berechnung zwischen Zeilen in Pandas Dataframe mit einer Distanz-matrix

Habe ich Folgendes Pandas DataFrame:

In [31]:
import pandas as pd
sample = pd.DataFrame({'Sym1': ['a','a','a','d'],'Sym2':['a','c','b','b'],'Sym3':['a','c','b','d'],'Sym4':['b','b','b','a']},index=['Item1','Item2','Item3','Item4'])
In [32]: print(sample)
Out [32]:
      Sym1 Sym2 Sym3 Sym4
Item1    a    a    a    b
Item2    a    c    c    b
Item3    a    b    b    b
Item4    d    b    d    a

und ich möchten, finden Sie den eleganten Weg, um den Abstand zwischen den einzelnen Item nach dieser Distanz-matrix:

In [34]:
DistMatrix = pd.DataFrame({'a': [0,0,0.67,1.34],'b':[0,0,0,0.67],'c':[0.67,0,0,0],'d':[1.34,0.67,0,0]},index=['a','b','c','d'])
print(DistMatrix)
Out[34]:
      a     b     c     d
a  0.00  0.00  0.67  1.34
b  0.00  0.00  0.00  0.67
c  0.67  0.00  0.00  0.00
d  1.34  0.67  0.00  0.00 

Beispielsweise den Vergleich Item1 zu Item2 vergleichen aaab -> accb -- mit der Distanz-matrix-das wäre 0+0.67+0.67+0=1.34

Ideal Ausgabe:

       Item1   Item2  Item3  Item4
Item1      0    1.34     0    2.68
Item2     1.34    0      0    1.34
Item3      0      0      0    2.01
Item4     2.68  1.34   2.01    0

InformationsquelleAutor cmiller8 | 2013-11-30

Schreibe einen Kommentar