Reputation: 2117
I'm working on iterating through to make an Ordered Dict for a bar chart.
In my working code for the year I get the months for the bar chart this way:
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
Then I write the months into an OrderedDict
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)])
Obviously this is not ideal. Therefore, I wrote a loop instead.
months=[]
for month in range(1, 13):
months.append(pivot_table[str(month)].astype(float).values)
months = OrderedDict([('Jan', 1), ('Feb', 2), ('Mar', 3), ('Apr',4), ('May',5), ('Jun',6),('Jul',7), ('Aug',8), ('Sep',9),('Oct',10),('Nov',11),('Dec',12)])
However, this is returning month as Type int, size 1, value 1 and months as a list of size zero []. Not what I want!
This is where I write the bar chart stuffs:
output_file("stacked_bar.html")
bar = Bar(months, Companies, title="Number of Calls Each Month", palette = palette, legend = "top_right", width = 1200, height=900)
bar.add_tools(hover)
show(bar)
Upvotes: 0
Views: 131
Reputation: 82929
You are just mapping month names to numbers. Instead, you have to use the element of the months
-list at that specific index. Also note that the indices in the list are still zero-based.
months_dict = OrderedDict([('Jan', months[0]),
('Feb', months[1]),
...,
('Dec', months[11])])
You could also make this a bit shorter by using a list-comprehension for the months and another list for the names and then just zipping those lists together, something like this:
months = [pivot_table[m].astype(float).values for m in range(1, 13)]
names = ["Jan", "Feb", ..., "Dec"]
months_dict = OrderedDict(list(zip(names, months)))
Upvotes: 1