def rep_sample(df, col, n, *args, **kwargs):
    nu = df[col].nunique()
m = len(df)
mpb = n // nu
mku = n - mpb * nu
fills = np.zeros(nu)
fills[:mku] = 1
sample_sizes = (np.ones(nu) * mpb + fills).astype(int)
gb = df.groupby(col)
sample = lambda sub_df, i: sub_df.sample(sample_sizes[i], *args, **kwargs)
subs = [sample(sub_df, i) for i, (_, sub_df) in enumerate(gb)]
return pd.concat(subs)
Python pandas: conditionally select a uniform sample from a dataframe