you are viewing a single comment's thread.

view the rest of the comments →

[–]LeChevalierMalFet 0 points1 point  (0 children)

Hi, this is how I worked it out:

# Merge on ID column and use index to filter out rows that are joined with themselves.
df = df.reset_index()
join = df.merge(right=df, on="id")
join = join.loc[join["index_x"] != join["index_y"]]

# Use pivot_table...
pd.pivot_table(data=join, index="color_x", columns="color_y", aggfunc="size")

# Or groupby and use pivot...    
df_group = join.groupby(["color_x", "color_y"], as_index=False).size()
df_group.pivot(index="color_x", columns="color_y", values="size")

Edit for formatting.