问题
I'd like to expand my rows by their cross product across multiple lists. The current logic I use is:
list = [['Yes', 'No', 'Maybe'], ['Yes', 'No', 'Maybe']]
index = pd.MultiIndex.from_product(list, names = ["column1", "column2"])
pd.DataFrame(index = index).reset_index()
which unfortunately will not work for more than one list. How would I be able to run the cartesian product of something that looks like this: [[['Yes', 'No', 'Maybe'], ['Yes', 'No', 'Maybe']],[['Yes', 'No', 'Maybe'], ['Yes', 'No', 'Maybe']]] and still have them only run for two columns. I'm looking to produce a crossproduct of 18 (2 * (3 ^ 2)).
回答1:
itertools.product allows you to create a cartesian product from an arbitrary number of iterables:
itertools.product(*lst)
来源:https://stackoverflow.com/questions/59429879/looping-through-lists-to-create-a-cartesian-product-by-row-pandas