简体   繁体   中英

Get all combinations of elements from two lists?

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.

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

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.tools.util.cartesian_product .

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM