I have been searching for an answer but I don't know what to search for so I'll ask here instead. I'm a beginner python and pandas enthusiast.
I have a dataset where i would like to produce a matrix from a column. The matrix should have the value of 1 if the value in the column and its transposed state is equal and 0 if its not.
input:
id x1
A 1
B 3
C 1
D 5
output:
A B C D
A 1 0 1 0
B 0 1 0 0
C 1 0 1 0
D 0 0 0 1
I would like to do this for six different columns and add the resulting matrixes into one matrix where the values range from 0-6 instead of just 0-1.
解决方案
Partly because there's as of yet no convenient cartesian join (whistles and looks away), I tend to drop down to numpy level and use broadcasting when I need to do things like this. IOW, because we can do things like this
>>> df.x1.values - df.x1.values[:,None]
array([[ 0, 2, 0, 4],
[-2, 0, -2, 2],
[ 0, 2, 0, 4],
[-4, -2, -4, 0]])
We can do
>>> pdf = pd.DataFrame(index=df.id.values, columns=df.id.values,
data=(df.x1.values == df.x1.values[:,None]).astype(int))
A B C D
A 1 0 1 0
B 0 1 0 0
C 1 0 1 0
D 0 0 0 1