Closed
Description
Many times it is desirable to be able to iterate over a small number of groups of a group by object. Currently there is no direct way to get a random sample of groups.
Similar to DataFrame.sample method, can we have Groupby.sample method which gives us a Dictionary{'group_name'->'group_labels'} containing n randomly selected groups?
Expected behaviour
import pandas as pd
df = pd.DataFrame([['Male', 1], ['Female', 3], ['Female', 2], ['Other', 1]],
columns=['gender', 'feature'])
grouped_df = df.groupby('gender')
# Desired feature
grouped_df.sample(n=2)
# {'Female': Int64Index([1, 2], dtype='int64'),
# 'Male': Int64Index([0], dtype='int64')}