Pandas - Creating Difference Matrix from Data Frame

前端 未结 3 1933
臣服心动
臣服心动 2020-12-06 03:23

I\'m trying to create a matrix to show the differences between the rows in a Pandas data frame.

import pandas as pd

data = {\'Country\':[\'GB\',\'JP\',\'US\         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 03:37

    I try improve Divakar comment:

    a = np.column_stack([df['Country'], np.subtract.outer(*[-df.Values]*2)])
    
    df = pd.DataFrame(a, columns=['Country'] + df['Country'].tolist())
    print (df)
      Country    GB    JP    US
    0      GB     0 -30.7 -14.5
    1      JP  30.7     0  16.2
    2      US  14.5 -16.2     0
    

提交回复
热议问题