cartesian product in pandas

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

    If you have a key that is repeated for each row, then you can produce a cartesian product using merge (like you would in SQL).

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

    Output:

       col1  col2  col3
    0     1     3     5
    1     1     3     6
    2     2     4     5
    3     2     4     6
    

    See here for the documentation: http://pandas.pydata.org/pandas-docs/stable/merging.html#brief-primer-on-merge-methods-relational-algebra

提交回复
热议问题