df = df.groupby(['groups', 'g', 'runs']).size().reset_index(name='0')
df1 = df.groupby(['groups', 'runs', '0']).size().unstack(level=[1, 2]).fillna(0
    ).astype(int)
pandas: counting numbers and combining results from apply