audit-labs/audit-tools
A collection of scripts, queries, and other goodies you can use in an audit.
clone: git clone https://gitbay.org/audit-labs/audit-tools.git
main: sampling/sample.py · raw
1"""
2Creates a sample from a CSV or Excel file based on user-defined SAMPLE_SIZE.
3
4NOTE: This is a minimal teaching snippet. For real fieldwork use the
5`audit_sample.py` CLI (or the `sampling_tool` package), which records the
6population hash, seed, method, and tool version in a manifest so the sample is
7reproducible and defensible. This file fixes a SEED only so the example itself is
8repeatable; it does not emit that provenance.
9"""
10
11# Import packages
12import pandas as pd
13
14# Define the sample size
15SAMPLE_SIZE = 25
16
17# A fixed seed makes the draw reproducible: same population + same seed => same
18# rows. Record the seed alongside any sample you rely on.
19SEED = 20260707
20
21# Import the data to a pandas DataFrame
22df = pd.read_csv("FILENAME_GOES_HERE.csv")
23
24# ALTERNATIVE: If you use Excel, use this instead. Supports xls, xlsx, xlsm,
25# xlsb, odf, ods and odt file extensions.
26# df = pd.read_excel("FILENAME_GOES_HERE.xlsx")
27
28# Print totals prior to sampling
29print("Dataframe size (rows, columns): ", df.shape)
30
31# Sample
32sample = df.sample(SAMPLE_SIZE, random_state=SEED)
33print("Sample size: ", SAMPLE_SIZE)
34print("Sample:\n", sample)
35
36# ALTERNATIVE: Replacement Samples
37#
38# If you want replacement samples (e.g., 10 samples & 3 replacements), you will
39# need to increase sample size to the total you want (e.g., 13). If that is
40# larger than the population, you will need to use the `replace=True` parameter.
41#
42# # Sample Size: 25 + 5 replacement samples
43# SAMPLE_SIZE = 30
44# sample = df.sample(SAMPLE_SIZE, replace=True, random_state=SEED)