Distance calculation between rows in Pandas Dataframe using a distance matrix

前端 未结 3 903
小鲜肉
小鲜肉 2020-12-31 10:04

I have the following Pandas DataFrame:

In [31]:
import pandas as pd
sample = pd.DataFrame({\'Sym1\': [\'a\',\'a\',\'a\',\'d\'],\'Sym2\':[\'a\',\'c\',\'b\',\'         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 10:41

    this is doing twice as much work as needed, but technically works for non-symmetric distance matrices as well ( whatever that is supposed to mean )

    pd.DataFrame ( { idx1: { idx2:sum( DistMatrix[ x ][ y ]
                                      for (x, y) in zip( row1, row2 ) ) 
                             for (idx2, row2) in sample.iterrows( ) } 
                     for (idx1, row1 ) in sample.iterrows( ) } )
    

    you can make it more readable by writing it in pieces:

    # a helper function to compute distance of two items
    dist = lambda xs, ys: sum( DistMatrix[ x ][ y ] for ( x, y ) in zip( xs, ys ) )
    
    # a second helper function to compute distances from a given item
    xdist = lambda x: { idx: dist( x, y ) for (idx, y) in sample.iterrows( ) }
    
    # the pairwise distance matrix
    pd.DataFrame( { idx: xdist( x ) for ( idx, x ) in sample.iterrows( ) } )
    

提交回复
热议问题