1# n: number of rows to be extracted randomly
2# random_state fixed for reproducibility
3# replace = True for extraction with replacement
4
5df.sample(n=3, random_state=42, replace=False)
1import numpy as np
2import pandas as pd
3df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
4
1>>> df.sample(frac=0.5, replace=True, random_state=1)
2 num_legs num_wings num_specimen_seen
3dog 4 0 2
4fish 0 0 8
5
1>>> df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
2... 'num_wings': [2, 0, 0, 0],
3... 'num_specimen_seen': [10, 2, 1, 8]},
4... index=['falcon', 'dog', 'spider', 'fish'])
5>>> df
6 num_legs num_wings num_specimen_seen
7falcon 2 2 10
8dog 4 0 2
9spider 8 0 1
10fish 0 0 8
11