OK so I have six possible values for data to be which are '32', '22', '12', '31', '21' and '11'. I have these stored as strings. Is it possible for python to sort through the data and just make six bins and show how many of each I have? Or do the inputs to a histogram HAVE to be numerical?
4 Answers
Did you consider using collections.Counter?
# python 2.7
>>> l = ['32', '22', '12', '31', '21', '11', '32']
>>> import collections
>>> collections.Counter(l)
Counter({'32': 2, '11': 1, '12': 1, '21': 1, '22': 1, '31': 1})
1 Comment
Catherine Georgia
Thanks, this looks like it would be super useful but sadly I seem to be on Python 2.6.6.
Assuming data is a list and you want to count the numbers in a bins. I will use bins as a dictionary.
bin = {'11': 0, '12': 0, '21': 0, '22': 0, '31': 0, '32': 0}
for element in data:
if element in bin: # Ignore other elements in data if any
bin[element] = bin[element] + 1
bins dictionary will have frequency of each element in data list. Now you can use bins to plot bar graph using graph plot library. May be you can use this post to check matplotlib usage for plotting bar graph.
listOfInts = map(int,listOfStrs)