I'd like to sample from from a grouped Pandas DataFrame where the group size is sometimes smaller than the N. In the following example, how could I sample 3 when the group size >= 3, otherwise all members of the group?
I am trying the following, but I get an error saying "Cannot take a larger sample than population when 'replace=False'".
import pandas as pd
df = pd.DataFrame({'some_key':[0,0,0,0,0,0,1,2,1,2],
'val': [0,1,2,3,4,5,6,7,8,9]})
gby = df.groupby(['some_key'])
gby.apply(lambda x: x.sample(n=3)).reset_index(drop=True)