I have a pandas dataframe that looks something like this:
Al01 BBR60 CA07 NL219 AAEAMEVAT MP NaN MP MP AAFEDLRLL NaN
It just matrix multiplication:
import pandas as pd df = pd.read_csv('data.csv',index_col=0, delim_whitespace=True) df2 = df.applymap(lambda x: int(not pd.isnull(x))) print df2.T.dot(df2)
Output:
Al01 BBR60 CA07 NL219 Al01 4 0 2 3 BBR60 0 1 0 0 CA07 2 0 3 3 NL219 3 0 3 4 [4 rows x 4 columns]