2017-09-05 4 views
2

タイトルが十分正確であることを願っています。列値の発生数をプロットする

とにかく、私の問題は、私は次のようなパンダのDFを持っていることです。

       Customer  Source CustomerSource 
0        Apple   A    141 
1        Apple   B    36 
2       Microsoft   A    143 
3        Oracle   C    225 
4         Sun   C    151 

これは、より大きなデータセットから派生DFで、意味はCustomerSourceの値は、それが蓄積されていますということですCustomerSourceのすべての出現の合計が、例えば、この場合には141のSoureAAppleの発生等SourceBとを有するCustomerOracle 225があります。

私がこれでやりたいことは、x軸にすべてCustomer s、そしてy軸にお互いに重なってCustomerSourceの値が積み重なった積み重ねられた棒グラフを作成したいのですか?以下の例に似ています。私がこれをどうやって進めるかについてのヒントは?

enter image description here

答えて

2

あなたはリシェイプためpivotまたはunstackを使用して、DataFrame.barことができます。

df.pivot('Customer','Source','CustomerSource').plot.bar(stacked=True) 

df.set_index(['Customer','Source'])['CustomerSource'].unstack().plot.bar(stacked=True) 

場合やペアCustomerで重複、Sourceを集約sumpivot_tableまたはgroupbyを使用します。

print (df) 
    Customer Source CustomerSource 
0  Apple  A    141 <-same Apple, A 
1  Apple  A    200 <-same Apple, A 
2  Apple  B    36 
3 Microsoft  A    143 
4  Oracle  C    225 
5  Sun  C    151 

df = df.pivot_table(index='Customer',columns='Source',values='CustomerSource', aggfunc='sum') 
print (df) 
Source   A  B  C 
Customer      
Apple  341.0 36.0 NaN <-141 + 200 = 341 
Microsoft 143.0 NaN NaN 
Oracle  NaN NaN 225.0 
Sun   NaN NaN 151.0 


df.pivot_table(index='Customer',columns='Source',values='CustomerSource', aggfunc='sum') 
    .plot.bar(stacked=True) 

df.groupby(['Customer','Source'])['CustomerSource'].sum().unstack().plot.bar(stacked=True) 

も可能スワップ・カラム:

df.pivot('Customer','Source','CustomerSource').plot.bar(stacked=True) 

g

df.pivot('Source', 'Customer','CustomerSource').plot.bar(stacked=True) 

g1

関連する問題