To grab random sample from a dataset in Python, you can use the pandas
library. Assuming your dataset is stored in a pandas DataFrame, you can use the sample
method to randomly select rows.
Here’s an example:
import pandas as pd
data = {
'Column1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'Column2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
}
your_dataset = pd.DataFrame(data)
# Grabbing random 5 rows
random_5_rows = your_dataset.sample(n=5, random_state=1)
# Display the result
print(random_5_rows)
In this example, n=
5 specifies the number of rows to sample, and random_state
is set to ensure reproducibility.
Column1 Column2
10 11 K
9 10 J
0 1 A
8 9 I
5 6 F