pandas 中df 對象自帶相關性計算方法corr() , 可以用來計算DataFrame對象中所有列之間的相關系數(包括pearson相關系數、Kendall Tau相關系數和spearman秩相關)。
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'A':np.random.randint(1, 100, 10),
'B':np.random.randint(1, 100, 10),
'C':np.random.randint(1, 100, 10)})
>>> df
A B C
0 5 91 3
1 90 15 66
2 93 27 3
3 70 44 66
4 27 14 10
5 35 46 20
6 33 14 69
7 12 41 15
8 28 62 47
9 15 92 77
>>> df.corr() # pearson相關系數
A B C
A 1.000000 -0.560009 0.162105
B -0.560009 1.000000 0.014687
C 0.162105 0.014687 1.000000
>>> df.corr('kendall') # Kendall Tau相關系數
A B C
A 1.000000 -0.314627 0.113666
B -0.314627 1.000000 0.045980
C 0.113666 0.045980 1.000000
>>> df.corr('spearman') # spearman秩相關
A B C
A 1.000000 -0.419455 0.128051
B -0.419455 1.000000 0.067279
C 0.128051 0.067279 1.000000
參考:https://blog.csdn.net/oh5w6hinug43jvrhhb/article/details/78389809