Sample fraction of each group
You can use GroupBy.apply with sample . You do not need to use lambda; apply accepts keyword arguments:
frac = .3 df.groupby('b').apply(pd.DataFrame.sample, frac=.3) ab b 0 6 7 0 1 0 1 1
If MultiIndex is not required, you can specify group_keys=False for groupby :
df.groupby('b', group_keys=False).apply(pd.DataFrame.sample, frac=.3) ab 6 7 0 2 3 1
Example N lines from each group
apply slowly. If your use case is for fetching a fixed number of rows, you can pre-mix the DataFrame and then use GroupBy.head .
df.sample(frac=1).groupby('b').head(2) ab 2 3 1 5 6 0 1 2 1 4 5 0
This is the same as df.groupby('b', group_keys=False).apply(pd.DataFrame.sample, n=N) , but faster :
%%timeit df.groupby('b', group_keys=False).apply(pd.DataFrame.sample, n=2)
source share