Change format of table
A = np.array(['A','B','C','A','B','A','B','C','A','A','B','C','D','E'], dtype=object).reshape(-1,1)
B = np.array([1, np.nan,np.nan,2,np.nan,3,np.nan,np.nan,4,5,np.nan,np.nan,np.nan,np.nan], dtype=object).reshape(-1,1)
arr = np.hstack((A,B)) 

df = pd.DataFrame(arr, columns=['A','B']) 
df.fillna(method='ffill',inplace=True)

cols = np.append(['num'],df['A'].unique())
data = pd.DataFrame(columns=cols)

data['num'] = pd.Series(df['B'].unique())

for i, row in data.iterrows():
    for j, column in row.iteritems():
        check = df[(df.B==i+1) &amp; (df.A==j)]
        if len(check) != 0:
            data.loc[i,j] = j

data.fillna('',inplace=True)
data


Some engineering might be needed on boundary conditions if pattern changes with new data.