df = pd.DataFrame({'D0': ['0', 'A', 'B', 'C'], 'D1': ['B', '0', 'C', 'D'],
    'D2': ['C', 'D', '0', 'A']})
df = df.replace('0', np.nan)
code = df.stack()
Pairwise Frequency Table for Multiple Columns in Python