Get all combinations of elements from two lists?

后端 未结 3 1975
滥情空心
滥情空心 2020-11-29 09:52

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:

相关标签:
3条回答
  • 2020-11-29 10:41

    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.

    0 讨论(0)
  • 2020-11-29 10:50

    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.

    0 讨论(0)
  • 2020-11-29 10:51

    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
    
    0 讨论(0)
提交回复
热议问题