# define empty dictionary
special_words_count = {}
# populate dictionary with sums
special_words_count["awesome"] = products["awesome"].sum()
special_words_count["great"] = products["great"].sum()
special_words_count["fantastic"] = products["fantastic"].sum()
special_words_count["amazing"] = products["amazing"].sum()
special_words_count["love"] = products["love"].sum()
special_words_count["horrible"] = products["horrible"].sum()
special_words_count["bad"] = products["bad"].sum()
special_words_count["terrible"] = products["terrible"].sum()
special_words_count["awful"] = products["awful"].sum()
special_words_count["wow"] = products["wow"].sum()
special_words_count["hate"] = products["hate"].sum()
# print values in descending order
# key=lambda x: x[1] # gets the VALUE (i.e. the second [0,1] element) of the PAIR x.
# reverse=True - set descending order
for key, value in sorted(special_words_count.items(), key=lambda x: x[1], reverse=True):
print value, key
42420 great
40277 love
3197 bad
2002 awesome
1305 amazing
1057 hate
873 fantastic
673 terrible
659 horrible
345 awful
131 wow
Another example of sorting the Dictionary
print name_value_dictionary
{'fantastic': 0.8913030903042991, 'love': 1.3998983430174596, 'bad': -0.9858273699287683, 'awesome': 1.0580088887848043, 'great': 0.8839378948979554, 'terrible': -2.090499984872614, 'amazing': 0.8928024225084649, '(intercept)': 1.367283152293053, 'horrible': -1.9965180055878113, 'awful': -1.7646995563132248, 'hate': -1.4091640627563906, 'wow': -0.0541450123332599}def input_method(my_tuple):
#print "*", my_tuple[1] # sanity check
return my_tuple[1]
tuples = name_value_dictionary.items() # key: value
for key, value in sorted(tuples, key=input_method, reverse=True):
print value, key
1.39989834302 love
1.36728315229 (intercept)
1.05800888878 awesome
0.892802422508 amazing
0.891303090304 fantastic
0.883937894898 great
-0.0541450123333 wow
-0.985827369929 bad
-1.40916406276 hate
-1.76469955631 awful
-1.99651800559 horrible
-2.09049998487 terrible