cartesian product in pandas

前端 未结 11 1806
再見小時候
再見小時候 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:08

    You could start by taking the Cartesian product of df1.col1 and df2.col3, then merge back to df1 to get col2.

    Here's a general Cartesian product function which takes a dictionary of lists:

    def cartesian_product(d):
        index = pd.MultiIndex.from_product(d.values(), names=d.keys())
        return pd.DataFrame(index=index).reset_index()
    

    Apply as:

    res = cartesian_product({'col1': df1.col1, 'col3': df2.col3})
    pd.merge(res, df1, on='col1')
    #  col1 col3 col2
    # 0   1    5    3
    # 1   1    6    3
    # 2   2    5    4
    # 3   2    6    4
    

提交回复
热议问题