Get exactly `n` unique randomly sampled rows per category in a Dataframe
Have you tried using DataFrameGroupBy.filter()  (I don't have recent pandas on this machine so I can't test it).
n = 4
df = pd.DataFrame({'category': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1],
                   'value' : range(12)})

# sample exactly x rows per category
df = df.groupby('category').filter(lambda x:x.size &gt;= n).sample(n)