Pandas DataFrame中两个非数字列之间的相关性

时间:2017-10-29 15:50:04

标签: python pandas matplotlib correlation

我从表中的SQL查询中获取数据到我的pandas Dataframe。数据如下:

     group phone_brand
0      M32-38          小米
1      M32-38          小米
2      M32-38          小米
3      M29-31          小米
4      M29-31          小米
5      F24-26        OPPO
6      M32-38          酷派
7      M32-38          小米
8      M32-38        vivo
9      F33-42          三星
10     M29-31          华为
11     F33-42          华为
12     F27-28          三星
13     M32-38          华为
14       M39+         艾优尼
15     F27-28          华为
16     M32-38          小米
17     M32-38          小米
18       M39+          魅族
19     M32-38          小米
20     F33-42          三星
21     M23-26          小米
22     M23-26          华为
23     M27-28          三星
24     M29-31          小米
25     M32-38          三星
26     M32-38          三星
27     F33-42          三星
28     M32-38          三星
29     M32-38          三星
...       ...         ...
74809  M27-28          华为
74810  M29-31         TCL

现在我想找到从此到列的相关性和频率。但这与Matplotlib的可视化有关。我试着像:

DataFrame.plot(style='o')
plt.show() 

现在我如何以最简单的方式可视化这种相关性?

2 个答案:

答案 0 :(得分:4)

快速获得相关性:

df.apply(lambda x: x.factorize()[0]).corr()

                group  phone_brand
group        1.000000     0.427941
phone_brand  0.427941     1.000000

热图

import seaborn as sns

sns.heatmap(pd.crosstab(df.group, df.phone_brand))

enter image description here

答案 1 :(得分:0)

使用pandas.factorize()方法可以通过识别不同的值来获取数组的数字表示形式。