来自Pandas的Dataframe的堆积条形图

时间:2015-07-07 19:55:08

标签: python pandas matplotlib plot bokeh

我试图制作一个堆积条形图,其中x轴是客户名称,y轴是调用次数,堆栈是月份。

我制作了一个如下所示的pivot_table:

pivot_table.head(3)

Out[23]: 
Month                      1   2   3   4   5   6   7   8   9   10  11  12
CompanyName                                                              
Company1   11   3   2   3   5   7   3   6   8   3   5   8
Company2   3   1   2  18   3   4   5   4   5   5   3   2
Company3   2   6   1   3   2   0   5   6   4   8   4   7

这是代码

df = pd.read_csv('MYDATA.csv')
df = df.set_index('recvd_dttm')
df.index = pd.to_datetime(df.index, format='%m/%d/%Y %H:%M')

result = df.groupby([lambda idx: idx.month, 'CompanyName']).agg(len).reset_index()
result.columns = ['Month', 'CompanyName', 'NumberCalls']
pivot_table = result.pivot(index='Month', columns='CompanyName', values='NumberCalls').fillna(0)
s = pivot_table.sum().sort(ascending=False,inplace=False)
pivot_table = pivot_table.ix[:,s.index[:40]]
pivot_table = pivot_table.transpose()



pivot_table = pivot_table.reset_index()
pivot_table['CompanyName'] = [str(x) for x in pivot_table['CompanyName']]
Companies = list(pivot_table['CompanyName'])
months = ["1","2","3","4","5","6","7","8","9","10","11","12"]
pivot_table = pivot_table.set_index('CompanyName')

并且我正在尝试绘图

ax = pivot_table.plot(kind='bar', title ="Bar chart",figsize=(15,10),legend=True, fontsize=12)
ax.set_xlabel("Company",fontsize=12)
ax.set_ylabel("Number of Calls",fontsize=12)

pivot_table.plot(kind='bar',stacked=True)

并尝试在散景中做这件事(对于这个情节非常重要):

months = OrderedDict(Jan=Jan, Feb=Feb, Mar=Mar, Apr=Apr, 

May=May,Jun=Jun,Jul=Jul,Aug=Aug,Sep=Sep,Oct=Oct,Nov=Nov,Dec=Dec)


# any of the following commented are also alid Bar inputs
#medals = pd.DataFrame(medals)
#medals = list(medals.values())

output_file("stacked_bar.html")

bar = Bar(months, Companies, title="Stacked bars", stacked=True)

show(bar)

对于所有三种绘图方法,请不断收到此错误:ValueError: Length mismatch: Expected axis has 27 elements, new values have 3 elements我已查看此ValueError但我仍然不了解此处发生了什么。

1 个答案:

答案 0 :(得分:-2)

就我而言,我正在阅读错误的数据文件。