-1

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)).

Fust
  • 19
  • 7
  • 3
    Can you update the question with the expected output that you are looking for? – smishra Dec 20 '19 at 18:35
  • 1
    expected output would be useful – Gius Dec 20 '19 at 18:37
  • 1
    Expected output, and making it so that every list isn't identical will greatly help people understand where the 18 products should come from. Perhaps you're looking for `[[*itertools.product(*x)] for x in lst]` – ALollz Dec 20 '19 at 18:52
  • They main point of interest is that I have a two lists nested within one list. How would I be able to run the code I included above over each list individually, and append them at the end? – Fust Dec 20 '19 at 19:01
  • Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide both sample input and output, because the way your question is worded is unclear as to what you actually need to accomplish – G. Anderson Dec 20 '19 at 19:43

1 Answers1

1

itertools.product allows you to create a cartesian product from an arbitrary number of iterables:

itertools.product(*lst)
scnerd
  • 5,836
  • 2
  • 21
  • 36