Trying to squeeze a little more performance out of pandas, we can use groupby
with size to get the counts. then use idxmax
to find the index values of the largest sub-groups. These indices will be the values we're looking for.
s = B.groupby(['MOST_FREQUENT', 'b']).size()
pd.DataFrame(
s.groupby(level='b').idxmax().values.tolist(),
columns=s.index.names
)
MOST_FREQUENT b
0 2 I
1 1 II
naive timing