sample
As of v0.20.0, you can use pd.DataFrame.sample
, which can be used to return a random sample of a fixed number rows, or a percentage of rows:
df = df.sample(n=k) # k rows
df = df.sample(frac=k) # int(len(df.index) * k) rows
For reproducibility, you can specify an integer random_state
, equivalent to using np.ramdom.seed
. So, instead of setting, for example, np.random.seed = 0
, you can:
df = df.sample(n=k, random_state=0)