更新:
是否可以对行/列进行排序,使计数最高的行位于顶部?在这种情况下,顺序为b,a,c
您可以通过IIUC这样的方式(在哪里)进行操作:
In [80]: x = df.pivot_table(index='customer1',columns='customer2',aggfunc='size',fill_value=0)
In [81]: idx = x.max(axis=1).sort_values(ascending=0).index
In [82]: idx
Out[82]: Index(['b', 'a', 'c'], dtype='object', name='customer1')
In [87]: sns.heatmap(x[idx].reindex(idx), annot=True)
Out[87]: <matplotlib.axes._subplots.AxesSubplot at 0x9ee3f98>
旧答案:
您可以使用模块中的heatmap()方法seaborn
:
In [42]: import seaborn as sns
In [43]: df
Out[43]:
customer1 customer2
0 a b
1 a c
2 a c
3 b a
4 b c
5 b c
6 c c
7 a a
8 b c
9 b c
In [44]: x = df.pivot_table(index='customer1',columns='customer2',aggfunc='size',fill_value=0)
In [45]: x
Out[45]:
customer2 a b c
customer1
a 1 1 2
b 1 0 4
c 0 0 1
In [46]: sns.heatmap(x)
Out[46]: <matplotlib.axes._subplots.AxesSubplot at 0xb150b70>
或带有注释:
In [48]: sns.heatmap(x, annot=True)
Out[48]: <matplotlib.axes._subplots.AxesSubplot at 0xc596d68>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句