Commit 1560a4e282
Unsigned
Layout: unified · split
sample.py added +30
| @@ -0,0 +1,30 @@ | |||
| 1 | # Import packages | ||
| 2 | import pandas as pd | ||
| 3 | |||
| 4 | # Define the sample size | ||
| 5 | sample_size = 25 | ||
| 6 | |||
| 7 | # Import the data to a pandas DataFrame | ||
| 8 | df = pd.read_csv("FILENAME_GOES_HERE.csv") | ||
| 9 | |||
| 10 | # ALTERNATIVE: If you use Excel, use this instead. Supports xls, xlsx, xlsm, | ||
| 11 | # xlsb, odf, ods and odt file extensions. | ||
| 12 | # df = pd.read_excel("FILENAME_GOES_HERE.xlsx") | ||
| 13 | |||
| 14 | # Print totals prior to sampling | ||
| 15 | print("Dataframe size (rows, columns): ", df.shape) | ||
| 16 | |||
| 17 | # Sample | ||
| 18 | sample = df.sample(sample_size) | ||
| 19 | print("Sample size: ", sample_size) | ||
| 20 | print("Sample:\n", sample) | ||
| 21 | |||
| 22 | # ALTERNATIVE: Replacement Samples | ||
| 23 | # | ||
| 24 | # If you want replacement samples (e.g., 10 samples & 3 replacements), you will | ||
| 25 | # need to increase sample size to the total you want (e.g., 13). If that is | ||
| 26 | # larger than the population, you will need to use the `replace=True` parameter. | ||
| 27 | # | ||
| 28 | # # Sample Size: 25 + 5 replacement samples | ||
| 29 | # sample_size = 30 | ||
| 30 | # sample = df.sample(30, replace=True) | ||