Pairwise matrix from a pandas dataframe

前端 未结 2 678
生来不讨喜
生来不讨喜 2020-12-19 21:01

I have a pandas dataframe that looks something like this:

             Al01   BBR60   CA07    NL219
AAEAMEVAT    MP      NaN     MP      MP 
AAFEDLRLL    NaN            


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 21:31

    It just matrix multiplication:

    import pandas as pd
    df = pd.read_csv('data.csv',index_col=0, delim_whitespace=True)
    df2 = df.applymap(lambda x: int(not pd.isnull(x)))
    print df2.T.dot(df2)
    

    Output:

               Al01  BBR60  CA07  NL219
    Al01      4      0     2      3
    BBR60     0      1     0      0
    CA07      2      0     3      3
    NL219     3      0     3      4
    
    [4 rows x 4 columns]
    

提交回复
热议问题