Say that I have a dataframe that looks like:
Name Group_Id AAA 1 ABC 1 CCC 2 XYZ 2 DEF 3 YYH 3
How could I randomly select one (or m
The solutions offered fail if a group has fewer samples than the desired sample size n. This addresses this problem:
n
n = 10 df.groupby('Group_Id').apply(lambda x: x.sample(min(n,len(x)))).reset_index(drop=True)