Skip to content Skip to sidebar Skip to footer

How To Draw A Heatmap In Pandas With Items That Don't Occur In Both Columns

In How to draw a graphical count table in pandas I asked how to draw a heatmap from input data such as: customer1,customer2 a,b a,c a,c b,a b,c b,c c,c a,a b,c b,c The answer was

Solution 1:

Try this:

In [129]: df
Out[129]:
  customer1 customer2
0         a         b
1         a         c
2         a         c
3         b         b
4         b         c
5         b         c
6         c         c
7         a         b
8         b         c
9         b         c

In [130]: x = df.pivot_table(index='customer1',columns='customer2',aggfunc='size',fill_value=0)

In [131]: idx = x.max(axis=1).sort_values(ascending=0).index

In [132]: cols = x.max().sort_values(ascending=0).index

In [133]: sns.heatmap(x[cols].reindex(idx), annot=True)
Out[133]: <matplotlib.axes._subplots.AxesSubplot at 0xbb22588>

enter image description here

Post a Comment for "How To Draw A Heatmap In Pandas With Items That Don't Occur In Both Columns"