Pandas: how to convert a list into a matrix grouped by a column?

前端 未结 1 1161
無奈伤痛
無奈伤痛 2021-01-20 11:23

I have a pandas dataframe where the first column (CUSTOMER) is the name of the customer and the customer\'s name is repeated once for every product the customer has purchase

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 12:10

    Self merge with crosstab

    d1 = df.merge(df, on='Customer').query('Product_x != Product_y')
    pd.crosstab(d1.Product_x, d1.Product_y)
    
    Product_y  A  B  C
    Product_x         
    A          0  2  1
    B          2  0  1
    C          1  1  0
    

    You can see this answer to get a better idea how to speed the crosstab up. The key insight for this problem was the self merging.

    0 讨论(0)
提交回复
热议问题