python - Key Error while making Stacked Bar Graph in Pandas -


i trying make stacked bar plot bokeh. keep getting keyerror: '1' can't figure out why. pivot_table looks this:

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 

here code:

from collections import ordereddict import pandas pd bokeh.charts import bar, output_file, show import datetime datetime   df = pd.read_csv('mydata.csv', usecols=[1, 16, 18]) #one companyname, 16 recvd_dttm, 18 machinetype  # filter countries @ least 1 medal , sort df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm'])  #only retrieve data before (ignore typos future dates) mask = df['recvd_dttm'] <= datetime.datetime.now() df = df.loc[mask] # first , last datetime final week of data  range_max = df['recvd_dttm'].max() range_min = range_max - datetime.timedelta(days=365)  # take slice final week of data df = df[(df['recvd_dttm'] >= range_min) &                 (df['recvd_dttm'] <= range_max)]    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) 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') pivot_table.to_csv('pivot_table.csv')     # months jan = pivot_table['1'].astype(float).values feb = pivot_table['2'].astype(float).values mar = pivot_table['3'].astype(float).values apr = pivot_table['4'].astype(float).values may = pivot_table['5'].astype(float).values jun = pivot_table['6'].astype(float).values jul = pivot_table['7'].astype(float).values aug = pivot_table['8'].astype(float).values sep = pivot_table['9'].astype(float).values oct = pivot_table['10'].astype(float).values nov = pivot_table['11'].astype(float).values dec = pivot_table['12'].astype(float).values # build dict containing grouped data 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)   # of following commented 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) 

i make fine in matplotlib, hovertool feature in bokeh. if did import matplotlib.pyplot plt , added these lines, stacked bar plot.

plot = pivot_table.plot(kind='bar',stacked=true) show(plot) 

i figure key error coming when months ordereddict? don't know how fix this. trying go off of example: http://bokeh.pydata.org/en/latest/docs/gallery/stacked_bar_chart.html

it seems though if use jan = pivot_table[1].astype(float).values instead of jan = pivot_table['1'].astype(float).values, works


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -