Deterministic previews
Create a reproducible subset for inspection or testing.
Pandas · Module
Randomly selects rows or columns from a DataFrame by count or fraction. Sampling can be weighted, repeated, made reproducible, and returned with a reset index.
Call sample on a DataFrame to randomly select rows by count or proportion. Choose n or frac, optionally provide replacement and weights, use random_state for repeatable selection, and set axis to sample columns instead; the result is a new DataFrame.
DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None, ignore_index=False)| Name | Type | Description |
|---|---|---|
n | int, optional | Number of items from the axis to return; mutually exclusive with frac. |
frac | float, optional | Fraction of axis items to return; mutually exclusive with n. |
replace | bool, default False | Whether the same row may be sampled more than once. |
weights | str or ndarray-like, optional | Sampling probabilities, supplied as a column name or array-like weights. |
random_state | int, array-like, BitGenerator, np.random.RandomState, np.random.Generator, optional | Seed or random generator used to make sampling reproducible. |
axis | {0 or ‘index’, 1 or ‘columns’, None}, default None | Axis to sample: 0 or index for rows, 1 or columns for columns; default is the statistical axis. |
ignore_index | bool, default False | When true, replace the returned index with 0 through n - 1. |
Series or DataFrame — a new object of the caller's type containing the randomly sampled items.
import pandas as pd
orders = pd.DataFrame({"id": [101, 102, 103, 104], "total": [12, 25, 18, 40]})
sample = orders.sample(n=2, random_state=7, ignore_index=True)
print(sample)Selects two rows reproducibly and relabels the result with a fresh zero-based index.
Create a reproducible subset for inspection or testing.
Represent a population unevenly during selection.
Select a random subset of features instead of observations.
It returns a new DataFrame containing randomly selected items from an axis. Use n for an item count or frac for a fraction, and these options are mutually exclusive.
Follow-up: How would you make a train/test sample repeatable? Pass a fixed random_state to each sampling operation and use non-overlapping selection logic if the partitions must be disjoint.
The default axis is the DataFrame's row axis. Set axis=1 or axis="columns" to sample columns instead.
Follow-up: What changes when sampling columns? The sampled items are columns, while the returned object remains a DataFrame with its row index preserved.
weights changes the selection probabilities; None gives equal weighting. A column name can be supplied for DataFrame row sampling, or an array-like weight vector can be used.
Follow-up: What should be checked before weighted sampling? Ensure the weights align with the sampled axis and represent valid, finite probabilities.
pandas.DataFramepandas.Series.samplepandas.DataFrame.headpandas.DataFrame.groupby