Created
October 17, 2019 18:03
-
-
Save D2theR/56dd8f9d6079b83205758d015faa4e98 to your computer and use it in GitHub Desktop.
Generate Excel .xlsx file using Pandas for DOX/DOE recipe/ingredient formulations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from pandas import DataFrame | |
from IPython.core.display import display, HTML | |
#List of ingredients | |
DOX_INGREDIENTS = [ | |
'Ingredient A', | |
'Ingredient B', | |
'Ingredient C', | |
'Ingredient D', | |
'Ingredient E' | |
] | |
# Function uses itertools, chain, and map to return a list of every combination of ingredients. | |
# Keep in mind if order of addition matters it's itertools.permutations you want, not combinations. | |
def all_combinations(any_list): | |
return list(itertools.chain.from_iterable( | |
map(list, itertools.combinations(any_list, i + 1)) | |
for i in range(len(any_list)))) | |
combos = all_combinations(DOX_INGREDIENTS) | |
EXPERIMENTS = {} | |
x = 0 | |
## Generate/parse a dictionary of results for each combination based on the total number of ingredients. | |
## Adds blank spaces to extend each list result per dictionary to allow for DataFrame object creation. | |
for XP in combos: | |
x += 1 | |
if len(XP) <= len(DOX_INGREDIENTS): | |
none_list = [' ' for n in range(len(DOX_INGREDIENTS)-len(XP))] | |
EXPERIMENTS[x] = XP | |
EXPERIMENTS[x].extend(none_list) | |
df = DataFrame(EXPERIMENTS, columns=[k for k in EXPERIMENTS]) | |
# display(df) #JuPyter Notebook | |
# print(df) # CLI | |
# or | |
# export_excel = df.to_excel (r'/home/dan/Desktop/tests/ghs_api/DOX.xlsx', index = None, header=True) #Don't forget to add '.xlsx' at the end of the path | |
# Hopefully somebody finds this useful, I know I did! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment