Directly calculating conditional averages in a Python Dictionary
Question: I ‘m guessing there is a better method to go about avergaing a dict in Python,
but I’m unsure how to go about it. At the moment I have a dict of dicts and I
am trying to find a better method of finding say the average age of company
car owners in a company. At the moment I am getting a correct result but I
think my method is inefficient, as I would search each NAME key in the Company
dict, check for Company Car == to ‘Yes’ and then drop the age of the employee
into a list, I would then do an average calculation on the list at the end of
the Company dict. I am sure there must be a better method than creating lists
and dropping values into it?

Here’s an example on my dict of dicts…

    
    
    Company{
    
    'NAME1': {''M_or_F': 'Male',
                  'AGE’: '24',
                 ‘DEPT’: ‘Finance',
          ‘Company Car’:’No’'}
    
    'NAME2': {''M_or_F': 'Male',
                  'AGE’: '52',
                 ‘DEPT’: ‘Marketing',
          ‘Company Car’:’Yes’'}
    
    'NAME3': {''M_or_F': 'Female',
                  'AGE’: '36',
                 ‘DEPT’: ‘Finance',
          ‘Company Car’:'Yes''}
    
    'NAME4': {''M_or_F': 'Male',
                  'AGE’: '28',
                 ‘DEPT’: ‘Finance',
          ‘Company Car’:’No’'}
    
    'NAME5': {''M_or_F': 'Female',
                  'AGE’: '23',
                 ‘DEPT’: ‘HR',
          ‘Company Car’:’Yes’'}
    } 
    

Any hints on how I could do away with lists and calculate directly from the
dictionary?

My current inefficient method is …

    
    
    CC_agelist = []
    
    for NAME in Company:
        if (Company[NAME][‘Company Car'] == 'Yes'):
            CC_agelist.append(int(Company[NAME]['AGE’]))
    
     #followed by an average calculation on CC_agelist
    


Answer: First, clean up the syntax of your data as follows:

    
    
    Company = {
    
    'NAME1': {'M_or_F': 'Male',
                 'AGE': '24',
                'DEPT': 'Finance',
         'Company Car': 'No'},
    
    'NAME2': {'M_or_F': 'Male',
                 'AGE': '52',
                'DEPT': 'Marketing',
         'Company Car': 'Yes'},
    
    'NAME3': {'M_or_F': 'Female',
                 'AGE': '36',
                'DEPT': 'Finance',
         'Company Car': 'Yes'},
    
    'NAME4': {'M_or_F': 'Male',
                 'AGE': '28',
                'DEPT': 'Finance',
         'Company Car': 'No'},
    
    'NAME5': {'M_or_F': 'Female',
                 'AGE': '23',
                'DEPT': 'HR',
         'Company Car': 'Yes'}
    }
    

Now you can use a list comprehension to get your list, followed by a simple
formula for calculating the mean:

    
    
    CC_agelist = [int(D['AGE']) for D in Company.itervalues() if D['Company Car'] == 'Yes']
    mean_CC_age = float(sum(CC_agelist)) / len(CC_agelist)
    

Or you can import numpy and do everything on one line:

    
    
    import numpy as np
    mean_CC_age = np.mean([int(D['AGE']) for D in Company.itervalues() if D['Company Car'] == 'Yes'])
    

