mahakanakala / toxins-in-skincare

A data analysis and visualization of skincare/makeup products using Seaborn & Plotly.
https://whats-in-your-skincare.vercel.app/about
0 stars 0 forks source link

Create a loop that will search the Ingredients column for an ingredient #1

Closed mahakanakala closed 1 year ago

mahakanakala commented 1 year ago

Currently, all the toxin ingredient occurrences are found by using this:

ethanolamine_occ = df[df['Ingredients'].str.contains('Ethanolamine')].shape[0]

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:

mahakanakala commented 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}.")