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, 5, 6]},
index=['sparrow', 'fox', 'spider', 'snake'])
df
Extract 3 random elements from the above Series:
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:
df.sample(n=2, weights='num_specimen_seen', random_state=1)