Python: How to split values and find averages by category

Python: How to split values and find averages by category … here is a solution to the problem.

Python: How to split values and find averages by category

I’m new to Python and programming and struggling to understand a problem. Any help is greatly appreciated.

I have a double-ended queue for values and categories :

deque([(3000.0, category1), (6000.0, category1), (8000.0, category2), (3000.0, category3), (7000.0, category3), (4500.0, category3)])

I’m trying to print the average of each value by category using a dictionary. For example, category 1 = (3000 + 6000)/2; category 2 = 8000/1; Category 3 = (3000 + 7000 + 4500)/3.

Is there an easy way to do this? So far, my only idea is to create a list of unique categories:

catetory_list = []
for i in deque:
    if i[1] not in category_list:
    category_list.append(i[1])

I

guess I’m missing something important here, but can’t seem to find the next step.

I’ve found some similar issues here, i.e. here and here , But I’m working on how to make it work for my problem.

Solution

You’re close, I think you need to construct a dict with values. Then average the values after :

Sample data:

my_dq = deque([
    (3000.0, 'category1'), (6000.0, 'category1'), (8000.0, 'category2'),
    (3000.0, 'category3'), (7000.0, 'category3'), (4500.0, 'category3')])

Code:

categories = {}
for i in my_dq:
    categories.setdefault(i[1], []).append(i[0])
categories = {k: sum(v) / len(v) for k, v in categories.items()}

Result:

{'category1': 4500.0, 'category3': 4833.3333, 'category2': 8000.0}

Related Problems and Solutions