Skip to content Skip to sidebar Skip to footer

Pandas Dataframe Matrix Based Calculation

I have a Pandas DataFrame as following. It shows how users have accessed pages p1 to p4 in each session. df = pd.DataFrame([[1,1,1,0,1],[2,1,1,0,1],[3,1,1,1,1],[4,0,1,0,1]]) df.col

Solution 1:

I think you are looking for numpy.outer:

In [10]: df1 = df.set_index('session')
         common = df1.dot(df1.T)

In [11]: df1.sum(1)
Out[11]: 
session
1          3
2          3
3          4
4          2
dtype: int64

In [12]: np.outer(*[df1.sum(1)] * 2)  # same as np.outer(df1.sum(1), df1.sum(1))
Out[12]: 
array([[ 9,  9, 12,  6],
       [ 9,  9, 12,  6],
       [12, 12, 16,  8],
       [ 6,  6,  8,  4]])

In [13]: np.sqrt(np.outer(*[df1.sum(1)] * 2))
Out[13]: 
array([[ 3.        ,  3.        ,  3.46410162,  2.44948974],
       [ 3.        ,  3.        ,  3.46410162,  2.44948974],
       [ 3.46410162,  3.46410162,  4.        ,  2.82842712],
       [ 2.44948974,  2.44948974,  2.82842712,  2.        ]])

In [14]: common / np.sqrt(np.outer(*[df1.sum(1)] * 2))
Out[14]: 
session         1         2         3         4
session                                        
1        1.000000  1.000000  0.866025  0.816497
2        1.000000  1.000000  0.866025  0.816497
3        0.866025  0.866025  1.000000  0.707107
4        0.816497  0.816497  0.707107  1.000000

Post a Comment for "Pandas Dataframe Matrix Based Calculation"