Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.sample

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.

How it works

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.

Syntax

DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None, ignore_index=False)

Parameters

NameTypeDescription
nint, optionalNumber of items from the axis to return; mutually exclusive with frac.
fracfloat, optionalFraction of axis items to return; mutually exclusive with n.
replacebool, default FalseWhether the same row may be sampled more than once.
weightsstr or ndarray-like, optionalSampling probabilities, supplied as a column name or array-like weights.
random_stateint, array-like, BitGenerator, np.random.RandomState, np.random.Generator, optionalSeed or random generator used to make sampling reproducible.
axis{0 or ‘index’, 1 or ‘columns’, None}, default NoneAxis to sample: 0 or index for rows, 1 or columns for columns; default is the statistical axis.
ignore_indexbool, default FalseWhen true, replace the returned index with 0 through n - 1.

Returns

Series or DataFrame — a new object of the caller's type containing the randomly sampled items.

Key points

  • DataFrame.sample randomly selects items from the chosen DataFrame axis and returns another DataFrame.
  • Set random_state to make the random selection repeatable across runs.
  • If neither n nor frac is supplied, one item is selected.
  • Use frac to request a proportion of the selected axis rather than an explicit count.
  • Leaving weights unset gives every candidate equal probability.
  • axis accepts a numeric axis or its name, allowing row or column sampling.

Common mistakes

  • Do not pass n and frac together.
  • With the default replace=False, requesting more items than the axis contains is invalid; enable replacement when repeats are needed.
  • Use ignore_index=True when the sampled output should not retain the original labels.

Example: Sample rows reproducibly

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.

When to use it

Deterministic previews

Create a reproducible subset for inspection or testing.

Weighted sampling

Represent a population unevenly during selection.

Column sampling

Select a random subset of features instead of observations.

Interview questions

QHow do you choose a fixed-size random sample?

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.

QHow can sample select columns?

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.

QHow does weighted sampling work?

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.DataFrame
  • pandas.Series.sample
  • pandas.DataFrame.head
  • pandas.DataFrame.groupby

Source: pandas.DataFrame.sample. Checked 2026-10-15.