Skip to content Skip to sidebar Skip to footer

Panda's : Matrix From Pd.crosstab()

I'm progressively learning pandas, I figured out that pd.crosstab() can do marvels but I've hard time to make it work in that case. I have a list of objects obj tagged with an int,

Solution 1:

You can use merge with crosstab and DataFrame.rename_axis:

df = df.merge(df, on='tag')
df = pd.crosstab(df.obj_x, df.obj_y).rename_axis(None).rename_axis(None, axis=1)
print (df)
   a  b  c  z
a  1  0  0  0
b  0  1  0  1
c  0  0  1  0
z  0  1  0  1

Post a Comment for "Panda's : Matrix From Pd.crosstab()"