Making a list based off of list of months in Python
Question: I am using Python to make lists. Should be easy! I don't know why I'm
struggling so much with this.

I have some data that I am counting up by date. There is a date column like
this:

    
    
    Created on
    5/1/2015
    5/1/2015
    6/1/2015
    6/1/2015
    7/1/2015
    8/1/2015
    8/1/2015
    8/1/2015
    

In this case, there would be 2 Units created in May, 2 Units in June, 1 Unit
in July, and 3 Units in August.

I want to reflect that in a list that starts in April ([April counts, May
counts, June counts, etc...]):

    
    
    NumberofUnits = [0, 2, 3, 1, 3, 0, 0, 0, 0, 0, 0, 0]  
    

I have a nice list of months

    
    
    monthnumbers
    
    Out[69]: [8, 5, 6, 7]
    

I also have a list with the `unitcounts = [2, 3, 1, 3]` I got this using
value_counts.

So it's a matter of making a list of zeroes and replacing parts with the
unitcount list, right?

For some reason all of my tries are either not making a list or making a list
with one zero in it.

    
    
    NumberofUnits = [0]*12
    
    for i in range(0,len(monthnumbers)):
        if  **monthnumbers[i] == (i+4):** **This part is wrong**       
            NumberofUnits.append(unitcounts[i])
            s = slice(0,i+1)
    

I also tried

    
    
    NumberofUnits = []
    for i in range(0, 12):
        if len(NumberofUnits) > i:
            unitcounts[i:]+unitcounts[:i]
            NumberofUnits.append(unitcounts[i])
            s = slice(0,i+1)
        else:
            unitcounts.append(0)
    

But this doesn't account for the fact that in this round my data starts with
May, so I need a zero in the first slot.


Answer: You can count entries using `collections.counter`

    
    
    from collections import Counter
    
    lines = ['5/1/2015', '5/1/2015', ..., '8/1/2015']
    month_numbers = [int(line.split("/")[0]) for line in lines]
    
    cnt = Counter(month_numbers)
    

If you already have counts you can replace above with

    
    
    from collections import defaultdict
    
    cnt = defaultdict(int, zip(monthnumbers, unitcounts))
    

and simply map to entries with (month_number - offset) mod 12:

    
    
    [x[1] for x in sorted([((i - offset) % 12, cnt[i]) for i in range(1, 13)])]
    

