Pandas - Creating Difference Matrix from Data Frame

前端 未结 3 1925
臣服心动
臣服心动 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条回答
  •  醉梦人生
    2020-12-06 03:47

    Option 1

    from itertools import product
    import pandas as pd
    DF=pd.DataFrame(list(product(df.Country, df.Country)), columns=['l1', 'l2'])
    df=df.set_index('Country')
    DF['v1']=DF.l1.map(df['Values'])
    DF['v2']=DF.l2.map(df['Values'])
    DF['DIFF']=DF['v2']-DF['v1']
    DF.pivot(index='l1', columns='l2', values='DIFF').fillna(0).rename_axis(None).rename_axis(None,1)
    Out[94]: 
          GB    JP    US
    GB   0.0 -30.7 -14.5
    JP  30.7   0.0  16.2
    US  14.5 -16.2   0.0
    

    Option 2 using apply

    A=df['Values'].apply(lambda x : df['Values']-x)
    A.columns=df.Country
    A['Country']=df.Country
    
    
    A
    Out[124]: 
    Country    GB    JP    US Country
    0         0.0 -30.7 -14.5      GB
    1        30.7   0.0  16.2      JP
    2        14.5 -16.2   0.0      US
    

提交回复
热议问题