我有以下矩阵:
import pandas as pd
df_test = pd.DataFrame({'TFD' : ['AA', 'SL', 'BB', 'D0', 'Dk', 'FF'],
'Snack' : ['1', '0', '1', '1', '0', '0'],
'Trans' : ['1', '1', '1', '0', '0', '1'],
'Dop' : ['1', '0', '1', '0', '1', '1']}).set_index('TFD')
df_test = df_test.astype(int)
matrix = df_test.T.dot(df_test)
print matrix
=>>>
Dop Snack Trans
Dop 4 2 3
Snack 2 3 2
Trans 3 2 4
我想要得到什么:
Dop-Snack 2
Snack-Trans 2
Trans-Dop 3
提前致谢!
答案 0 :(得分:2)
假设对配对顺序没有特殊要求:
import itertools
for c, r in itertools.combinations(matrix.columns, 2):
print("{}-{}\t{}".format(c, r, matrix.loc[c, r]))
# Dop-Snack 2
# Dop-Trans 3
# Snack-Trans 2
https://docs.python.org/2/library/itertools.html#itertools.combinations