Monday, 15 September 2014

Directly calculating conditional averages in a Python Dictionary -


I think there is a better way to learn about Python in Python, but I'm unsure how to go about this. At the moment I have a decict and I am trying to find a better way to know that the company has the average age of the car owner in the company. At the moment I am getting the right result, but I feel that my method is inefficient because I look for the name of every name in the company's name, check the 'yes' for the company car and then the age of the employee Leave a list, then I have to calculate an average on the list at the end of the company. Dict I am sure there should be better way of making lists and leaving its values?

Here is an example for my attributes ...

  Company {'NAME1': '' MA '': 'Male', 'Age': ' 24 ',' DEPT ':' Finance ',' Company Car ':' No '' '' NAME2 ': {' 'M_OR_F' ':' Male '' AG ':' 52 ',' DEPT ':' Marketing ' 'Company': 'Yes' '}}' 'NAME3': {'' M_OR_F '': 'Women', 'Age': '36', 'DEPT' '' '' '' ',' Company ':' Yes '' '' NAME4 ': {' 'MO': 'NO' '}' NAME5 ': {' 'M_OR_F' ':' Woman ',' Age ':' 23 ',' DEPT ':' HR ',' Company car ':' yes ''}}  

How can I overcome the signs and I Sector I can calculate the dictionary?

My current disable method is ...

  CC_agelist = [] For company in NAME: if (Company [NAME] ['Company car'] = = 'Yes'): CC_agelist.append (int (company [NAME] ['AGE'])) followed by average calculation on CC_agelist  

  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_ '' '' 'Name' ':' '' '' '' ',' '' ',' '' '' '' '' '' '' '' '' 28 ',' DEPT ':' Finance ',' Company Car ':' No ',' NAME5 ': {' M_or_F ':' Female ',' Age ':' 23 'DEPT': 'HR', 'Company Car': 'Yes'}}  

Now you can use a list understanding to get your list, after that A simple formula for mean: company for D.C_agelist = [int (D ['AGE']) D [edit] If D ['company car'] == 'yes' ] Mean_CC_j = float (amount (cc _Egelist) / Lane (CC_Egelist)

Or you can import oval and do everything in one line:

np for company's d Import_date_CC_age = np.mean (as [int (D ['AGE']). If ([company car]] = '' yes']) =

No comments:

Post a Comment