pandas
data analysis
python
dataframe
random selection

Random row selection in Pandas dataframe

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Random row selection in pandas is simple to write but easy to misuse in real analysis workflows. The DataFrame.sample API covers most use cases, yet reproducibility, class balance, and weighting decisions determine whether the result is statistically useful. A solid approach treats sampling as part of data quality, not as a one-line convenience.

Start with Deterministic Sampling

Use sample with n or frac, and set random_state whenever you need reproducible outputs across reruns.

python
1import pandas as pd
2
3# Example dataset
4df = pd.DataFrame({
5    'id': range(1, 11),
6    'segment': ['A', 'A', 'B', 'C', 'A', 'B', 'C', 'C', 'B', 'A'],
7    'value': [11, 20, 14, 18, 30, 16, 9, 25, 13, 22]
8})
9
10sample_n = df.sample(n=3, random_state=42)
11sample_frac = df.sample(frac=0.4, random_state=42)
12
13print(sample_n)
14print(sample_frac)

Without a fixed seed, reports may differ across runs even when source data is unchanged, which makes debugging and peer review harder.

Choose Replacement Mode Intentionally

By default, pandas uses replace=False, so the same row cannot be selected twice. Set replace=True only when bootstrap-style sampling is intentional.

python
bootstrap = df.sample(n=15, replace=True, random_state=7)
print(bootstrap['id'].value_counts().sort_index())

Replacement sampling can silently violate downstream uniqueness assumptions. If later logic expects one row per id, add checks immediately after sampling.

Apply Weighted Sampling for Controlled Bias

When some records should be sampled more often, use the weights parameter with a numeric column.

python
weighted_df = df.assign(weight=[1, 1, 3, 2, 1, 2, 1, 1, 3, 1])
weighted_sample = weighted_df.sample(n=4, weights='weight', random_state=11)
print(weighted_sample[['id', 'segment', 'weight']])

Rules to enforce:

  • weights must be nonnegative
  • weights should align with the dataframe index
  • missing weight values should be handled explicitly

If weights are malformed, results can be distorted or sampling will fail at runtime.

Use Group-Aware Sampling for Balanced Subsets

Global random sampling can underrepresent minority groups. For model evaluation, stratified sampling keeps group presence stable.

python
1stratified = (
2    df.groupby('segment', group_keys=False)
3      .apply(lambda g: g.sample(n=1, random_state=5))
4      .reset_index(drop=True)
5)
6print(stratified)

For proportional stratification, compute per-group sample sizes first, then sample each group with its own n value.

Validate Sample Quality Before Analysis

Sampling should include quick verification, not just row extraction. Compare distribution, duplicates, and summary statistics between sampled and full data.

python
1sampled = df.sample(frac=0.5, random_state=123)
2
3dist_full = df['segment'].value_counts(normalize=True).rename('full')
4dist_sample = sampled['segment'].value_counts(normalize=True).rename('sample')
5print(pd.concat([dist_full, dist_sample], axis=1).fillna(0))
6
7print('duplicates_in_sample:', sampled['id'].duplicated().sum())
8print('mean_full:', df['value'].mean(), 'mean_sample:', sampled['value'].mean())

These lightweight checks catch accidental skew early and reduce the chance of misleading conclusions.

Performance Tips for Large DataFrames

On large data:

  • sample only required columns when possible
  • avoid unnecessary copies before sampling
  • consider pushing sampling to the database engine if data originates there

For repeat workflows, package sampling in a helper function with explicit parameters for seed, replacement, and stratification strategy.

python
1def take_sample(frame, frac, seed, by=None):
2    if by is None:
3        return frame.sample(frac=frac, random_state=seed)
4
5    return (
6        frame.groupby(by, group_keys=False)
7             .apply(lambda g: g.sample(frac=frac, random_state=seed))
8             .reset_index(drop=True)
9    )

Shared utilities reduce drift between notebooks and production pipelines.

Common Pitfalls

  • Omitting random_state when reproducibility matters.
  • Using replace=True unintentionally and introducing duplicates.
  • Applying invalid or index-misaligned weights.
  • Ignoring class imbalance when sampling for training or evaluation.
  • Skipping validation checks before using sampled data in decisions.
  • Mixing different sampling strategies across team notebooks without documentation.

Summary

  • Use DataFrame.sample with explicit parameters, not defaults by habit.
  • Set deterministic seeds for reproducible analysis and debugging.
  • Choose replacement and weighting only when statistically justified.
  • Apply group-aware sampling when balanced representation is required.
  • Validate sampled output against source distributions before interpretation.
  • Centralize sampling logic to keep team workflows consistent.
  • Document your sampling policy near model metrics so result comparisons stay meaningful across releases.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.