Stratified Sampling in Pandas

df = pd.read_csv("path/to/data.csv")

df.groupby('Category', group_keys=False).apply(lambda x: x.sample(frac=0.1, random_state=42))

Revision #2
Created 2022-11-10 11:21:06 UTC by James
Updated 2024-02-11 16:27:03 UTC by James