2016-09-01 5 views
2

私は、2つのカラムcustomer1customer2という文字列値を持つデータフレームdfを持っています。私はそれらの2つの列からの各ペアのカウント数の正方形のグラフィック表現を作成したいと思います。パンダにグラフィカルカウントテーブルを描画する方法

私は私にカウントを与える

df[['customer1', 'customer2']].value_counts() 

を行うことができます。結果から

enter image description here

:しかし、どのように私は少しのように見える何かを作ることができますか?

私は実際のデータセットを提供できませんが、ここではcsvに3つのラベルがあるおもちゃの例です。

customer1,customer2 
a,b 
a,c 
a,c 
b,a 
b,c 
b,c 
c,c 
a,a 
b,c 
b,c 
+1

[seaborn.heatmap](https://stanford.edu/~mwaskom/software/seaborn/examples/heatmap_annotation.html)を見...ルックス – MaxU

+0

@MaxU良い。あなたは、パンダのデータフレームからsns.heatmapが受け入れることができるものに行く方法を知っていますか? – eleanora

+0

サンプルデータセットを提供してください – MaxU

答えて

2

UPDATE:

それが最高のcount行が先頭に をしているので、行/列をソートすることは可能ですか?

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> 

enter image description here

OLD答え:

この場合の順序は、

IIUC cは、あなたがこの方法()それを行うことができ、B、されるだろうseabornモジュールのheatmap()メソッドを使用できます。

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> 

enter image description here

又は注釈付き:

In [48]: sns.heatmap(x, annot=True) 
Out[48]: <matplotlib.axes._subplots.AxesSubplot at 0xc596d68> 

enter image description here

+0

ありがとうございます。行数/列数を並べ替えることは可能ですか?この場合、順序はb、a、cとなります。 – eleanora

+0

@eleanora、アップデートセクション – MaxU

+0

をご確認ください。ありがとうございます。私は、行と列が同じ方法でソートされることを意味しました。したがって、この場合、行はb、a、cとなり、列も同様になります。 – eleanora

0

@MaxUはseaborn.heatmapが動作するはず、上述したように。 Pandas DataFrameを入力として使用できるようです。

seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, ax=None, xticklabels=True, yticklabels=True, mask=None, **kwargs)

https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.heatmap.html#seaborn.heatmap

+0

データフレームは私が仮定したカウントデータを保持しなければならないので、まだ計算する必要がありますか? – eleanora

関連する問題