I have a pandas DataFrame like this:
And I would like to create a symmetrical matrix like this:
Where the value is actually the length of the intersection of the two lists. Therefore I have made this function:
def intersectSize(l1, l2):
return len(set(l1) & set(l2))
Is there a function out there that would resemble this one:
def createSymMatrix(array, func):
...
return matrix
where array
is my initial dataframe and func
is the intersectSize
function?
EDIT: figured it out with this two liner:
array = [[len(set(l1)&set(l2)) for l1 in df]] for l2 in df]
adj = pd.DataFrame(data=array, index=df.index, columns=df.index)
I think you need this,
r=[]
for val in list(itertools.product(df[0].values,df[0].values)):
r.append( len(set(val[0])&set(val[1])) )
print pd.DataFrame(np.array(r).reshape(len(df),-1))
Using List Comprehension:
t= [len(set(val[0])&set(val[1])) for val in list(itertools.product(df[0].values,df[0].values))]
print pd.DataFrame(np.array(t).reshape(len(df),-1))
Output:
0 1 2 3
0 3 0 0 1
1 0 1 0 1
2 0 0 2 0
3 1 1 0 2