Closed mahakanakala closed 1 year ago
Used a library to store the toxin names and toxin occurrences in each toxicity category in a for loop:
low_list = [('ethanolamine_occ', 'Ethanolamine'),
('oxybenzone_occ', 'Oxybenzone'),
('resorcinol_occ', 'Resorcinol'),
('formaldehyde_occ', 'Formaldehyde'),
('diethanolamine_occ', 'Diethanol'),
('silanes_occ', 'Silane'),
('octinaxate_occ', 'Octinaxate')]
# make an array that stores the occurances later for dataframe creation
low_occurrences_list = []
# make an array that stores the toxin names later for dataframe creation
low_hazard_list = []
# Iterate over the list and create the variables dynamically
for var, search_str in low_list:
# Create the variable with the specified name and value
globals()[var] = df[df['Ingredients'].str.contains(search_str)]
low_occurrences = len(globals()[var])
# appends each occurance to the array
low_occurrences_list.append(low_occurrences)
# appends each toxin name to the array
low_hazard_list.append(search_str)
# Calculate the number of occurrences using the len function
low_occurrences = len(globals()[var])
# Print the number of occurrences
print(f"{search_str}: {low_occurrences}.")
Currently, all the toxin ingredient occurrences are found by using this:
This is done for each toxin, but this reduces the efficiency and space complexity of the notebooks. Implementing a loop will solve this issue and make the code more readable
Options: