pandas.DataFrame.sample

pandas 3.1.1 · DataFrame · method

Randomly select rows, entries or columns. Set random_state for repeatable results.

Call syntax

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

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
n Optional
Positional or keyword
Default None
int

Choose how many entries or positions to use.

frac Optional
Positional or keyword
Default None
float

Set the fraction of the input to sample.

replace Optional
Positional or keyword
Default False
bool

Choose whether the same entry can be sampled multiple times.

weights Optional
Positional or keyword
Default None
str or ndarray-like

Supply values representing relative importance for sampling or calculation.

random_state Optional
Positional or keyword
Default None
int, array-like, BitGenerator, np.random.RandomState, np.random.Generator

Supply a seed or random generator to make sampling reproducible.

axis Optional
Positional or keyword
Default None
{0 or 'index', 1 or 'columns', None}

Choose which axis the operation acts on. The accepted axis labels depend on the receiver.

ignore_index Optional
Positional or keyword
Default False
bool

Give the resulting axis new integer labels where supported.

Returns

Series or DataFrame