cartesian product in pandas

前端 未结 11 1824
再見小時候
再見小時候 2020-11-21 23:35

I have two pandas dataframes:

from pandas import DataFrame
df1 = DataFrame({\'col1\':[1,2],\'col2\':[3,4]})
df2 = DataFrame({\'col3\':[5,6]})     

11条回答
  •  面向向阳花
    2020-11-22 00:10

    As an alternative, one can rely on the cartesian product provided by itertools: itertools.product, which avoids creating a temporary key or modifying the index:

    import numpy as np 
    import pandas as pd 
    import itertools
    
    def cartesian(df1, df2):
        rows = itertools.product(df1.iterrows(), df2.iterrows())
    
        df = pd.DataFrame(left.append(right) for (_, left), (_, right) in rows)
        return df.reset_index(drop=True)
    

    Quick test:

    In [46]: a = pd.DataFrame(np.random.rand(5, 3), columns=["a", "b", "c"])
    
    In [47]: b = pd.DataFrame(np.random.rand(5, 3), columns=["d", "e", "f"])    
    
    In [48]: cartesian(a,b)
    Out[48]:
               a         b         c         d         e         f
    0   0.436480  0.068491  0.260292  0.991311  0.064167  0.715142
    1   0.436480  0.068491  0.260292  0.101777  0.840464  0.760616
    2   0.436480  0.068491  0.260292  0.655391  0.289537  0.391893
    3   0.436480  0.068491  0.260292  0.383729  0.061811  0.773627
    4   0.436480  0.068491  0.260292  0.575711  0.995151  0.804567
    5   0.469578  0.052932  0.633394  0.991311  0.064167  0.715142
    6   0.469578  0.052932  0.633394  0.101777  0.840464  0.760616
    7   0.469578  0.052932  0.633394  0.655391  0.289537  0.391893
    8   0.469578  0.052932  0.633394  0.383729  0.061811  0.773627
    9   0.469578  0.052932  0.633394  0.575711  0.995151  0.804567
    10  0.466813  0.224062  0.218994  0.991311  0.064167  0.715142
    11  0.466813  0.224062  0.218994  0.101777  0.840464  0.760616
    12  0.466813  0.224062  0.218994  0.655391  0.289537  0.391893
    13  0.466813  0.224062  0.218994  0.383729  0.061811  0.773627
    14  0.466813  0.224062  0.218994  0.575711  0.995151  0.804567
    15  0.831365  0.273890  0.130410  0.991311  0.064167  0.715142
    16  0.831365  0.273890  0.130410  0.101777  0.840464  0.760616
    17  0.831365  0.273890  0.130410  0.655391  0.289537  0.391893
    18  0.831365  0.273890  0.130410  0.383729  0.061811  0.773627
    19  0.831365  0.273890  0.130410  0.575711  0.995151  0.804567
    20  0.447640  0.848283  0.627224  0.991311  0.064167  0.715142
    21  0.447640  0.848283  0.627224  0.101777  0.840464  0.760616
    22  0.447640  0.848283  0.627224  0.655391  0.289537  0.391893
    23  0.447640  0.848283  0.627224  0.383729  0.061811  0.773627
    24  0.447640  0.848283  0.627224  0.575711  0.995151  0.804567
    

提交回复
热议问题