Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to generate all possible combinations from list elements in Python having Pandas DataFrames in list?

Good afternoon!
I have a list of lists in Python. Example:

mylist = [['a', 'b', 'c'],
          [1, 2],
          [df1, df2]]

df1, df2 are Pandas DataFrames. I want to generate result similar to itertools.product(*mylist).

The problem is that Pandas DataFrames are iterables themselves, so the result which product returns is not what I want. I want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[('a', 1, df1),
 ('a', 1, df2),
 ('a', 2, df1),
 ('a', 2, df2),
 ('b', 1, df1),
 ('b', 1, df2),
 ('b', 2, df1),
 ('b', 2, df2),
 ('c', 1, df1),
 ('c', 1, df2),
 ('c', 2, df1),
 ('c', 2, df2)]

But product, of course, can not generate the desired ouptut, since it begins to iterate over df1 and df2 columns. How can I solve this problem in an elegant and Pythonic way?

Any help appreciated

>Solution :

Are you sure? product() iterates over the iterables passed to, it but only one level deep.

>>> from itertools import product
>>> mylist = [[1, 2], ['a', 'b'], [[4, 6], [8, 9]]]
>>> for x in product(*mylist):
...     print(x)
(1, 'a', [4, 6])
(1, 'a', [8, 9])
(1, 'b', [4, 6])
(1, 'b', [8, 9])
(2, 'a', [4, 6])
(2, 'a', [8, 9])
(2, 'b', [4, 6])
(2, 'b', [8, 9])

See? That [4, 6] and [8, 9] are themselves iterables is irrelevant to product().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading