A_val, nunique_B = zip(*[(k, len(Counter(v.index.labels[v.index.names.index
    ('B')]))) for k, v in ex.groupby(level='A')])
pd.Series(nunique_B, index=pd.Int64Index(A_val, name='A'))
Counting unique index values in Pandas groupby