cartesian product in pandas

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

    You can use numpy as it could be faster. Suppose you have two series as follows,

    s1 = pd.Series(np.random.randn(100,))
    s2 = pd.Series(np.random.randn(100,))
    

    You just need,

    pd.DataFrame(
        s1[:, None] @ s2[None, :], 
        index = s1.index, columns = s2.index
    )
    

提交回复
热议问题