Feature Request: Sample method for Groupby objects · Issue #31775 · pandas-dev/pandas (original) (raw)
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)