Get exactly `n` unique randomly sampled rows per category in a Dataframe I want to get exactly n unique randomly sampled rows per category in a Dataframe. This proved to be involve more steps than the description would lead you to believe.
n = 4
df = pd.DataFrame({'category': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1],
                   'value' : range(12)})


category_counts = df['category'].value_counts()
categories_with_count_above_threshold = category_counts[category_counts &gt;= n].index

# rows with infrequent categories are filtered out
df = df[df['category'].isin(categories_with_count_above_threshold)]

# sample exactly x rows per category
df = df.groupby('category').sample(n)

This goes through the whole DataFrame quite a few times. With a bigger DataFrame this can become quite time consuming. Is there a way to simplify this expression?
PS: requires pandas &gt;= 1.1 for the DataFrameGroupBy.sample
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)