mardi 3 octobre 2017

how to use if statement for iterating through mutable datatypes to remove dividing by zero

I am analyzing 2016 survey data taken by FreeCodeCamp. http://ift.tt/2kkRGyn

in particular, 2016-new-coder-survey/clean-data/2016-FCC-New-Coders-Survey-Data.csv

I am normalizing a plot by dividing the net recommendation amount (a difference) by the total (#sum of recommendations) for each x-value (Age).

for a few ages, there is zero recommendations, thus I end up dividing by zero and receiving ZeroDivisionError. I've already found a way to do it. but for the sake of learning, how could i accomplish fixing this with an if statement? could you explain what is wrong with the following code?

here is just all my variables, included for completeness.

data_file = pd.read_csv('FCC_New_Coders_Survey_Data.csv', dtype={'AttendedBootcamp': float, 'CodeEventOther': object, 'JobRoleInterestOther': object})
AttendedBootcamp = data_file['AttendedBootcamp']
BootcampFullJob = data_file['BootcampFullJobAfter']
BootcampRecommend = data_file['BootcampRecommend']
Age = data_file['Age']
JobYes = data_file[data_file.BootcampFullJobAfter == 1]
JobNo = data_file[data_file.BootcampFullJobAfter == 0]
RecYes = data_file[data_file.BootcampRecommend == 1]
RecNo = data_file[data_file.BootcampRecommend == 0]
RecYesJobYes = data_file[data_file.BootcampRecommend == 1][data_file.BootcampFullJobAfter == 1 ]
RecNoJobYes = data_file[data_file.BootcampRecommend == 0][data_file.BootcampFullJobAfter == 1 ]
RecYesJobNo = data_file[data_file.BootcampRecommend == 1][data_file.BootcampFullJobAfter == 0 ]
RecNoJobNo = data_file[data_file.BootcampRecommend == 0][data_file.BootcampFullJobAfter == 0 ]

here lies my failed code

numerator = [len(RecYesJobYes[RecYesJobYes.Age == i]) - len(RecNoJobYes[RecNoJobYes.Age == i]) for i in range(16, 60)]
denomerator = [len(RecYesJobYes[RecYesJobYes.Age == i]) + len(RecNoJobYes[RecNoJobYes.Age == i]) for i in range(16, 60)]

here is my attempt at exception handling

try:
    bananasplit = [int(m) / int(b) for b,m in zip(denomerator, numerator)]
except ZeroDivisionError:
        b = 1 in banana

here is my if statement

for b in [int(m) / int(b) for b,m in zip(denomerator, numerator)][:]:
    if ZeroDivisionError:
        b = 1 in banana

Aucun commentaire:

Enregistrer un commentaire