Get all combinations of elements from two lists?

Question:

If I have two lists

l1 = ['A', 'B']

l2 = [1, 2]

what is the most elegant way to get a pandas data frame which looks like:

+-----+-----+-----+
|     | l1  | l2  |
+-----+-----+-----+
|  0  | A   | 1   |
+-----+-----+-----+
|  1  | A   | 2   |
+-----+-----+-----+
|  2  | B   | 1   |
+-----+-----+-----+
|  3  | B   | 2   |
+-----+-----+-----+

Note, the first column is the index.

Asked By: K.Chen

||

Answers:

use product from itertools:

>>> from itertools import product
>>> pd.DataFrame(list(product(l1, l2)), columns=['l1', 'l2'])
  l1  l2
0  A   1
1  A   2
2  B   1
3  B   2
Answered By: behzad.nouri

As an alternative you can use pandas’ cartesian_product (may be more useful with large numpy arrays):

In [11]: lp1, lp2 = pd.core.reshape.util.cartesian_product([l1, l2])

In [12]: pd.DataFrame(dict(l1=lp1, l2=lp2))
Out[12]:
  l1  l2
0  A   1
1  A   2
2  B   1
3  B   2

This seems a little messy to read in to a DataFrame with the correct orient…

Note: previously cartesian_product was located at pd.core.reshape.util.cartesian_product.

Answered By: Andy Hayden

You can also use the sklearn library, which uses a NumPy-based approach:

from sklearn.utils.extmath import cartesian

df = pd.DataFrame(cartesian((L1, L2)))

For more verbose but possibly more efficient variants see Numpy: cartesian product of x and y array points into single array of 2D points.

Answered By: jpp

You can use the function merge:

df1 = pd.DataFrame(l1, columns=['l1'])
df2 = pd.DataFrame(l2, columns=['l2'])

df1.merge(df2, how='cross')

Output:

  l1  l2
0  A   1
1  A   2
2  B   1
3  B   2
Answered By: Mykola Zotko
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.