You can use the sample * method:
In [11]: df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"]) In [12]: df.sample(2) Out[12]: AB 0 1 2 2 5 6 In [13]: df.sample(2) Out[13]: AB 3 7 8 0 1 2
* In one of the DataFrames sections.
Note. If you have a larger sample size, the size of the DataFrame will cause an error if you do not try with a replacement.
In [14]: df.sample(5) ValueError: Cannot take a larger sample than population when 'replace=False' In [15]: df.sample(5, replace=True) Out[15]: AB 0 1 2 1 3 4 2 5 6 3 7 8 1 3 4
Andy hayden
source share