Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [8, 2, 1, 6]},
index=['sparrow', 'cat', 'spider', 'snake'])
df
Extract 3 random elements from the Series df['num_legs']: Note that we use random_state
to ensure the reproducibility of the examples.
df['num_legs'].sample(n=3, random_state=1)
A random 50% sample of the DataFrame with replacement:
df.sample(frac=0.5, replace=True, random_state=1)
Using a DataFrame column as weights. Rows with larger value in the num_specimen_seen
column are more likely to be sampled.
df.sample(n=2, weights='num_specimen_seen', random_state=1)