Skip to content

LazyFrame.sample

sample

sample(
    n: int | None = None,
    frac: float | None = None,
    random_state: int | None = None,
) -> LazyFrame

Return a random sample of items from the LazyFrame.

Parameters:

Name Type Description Default
n int | None

Number of items to return. Cannot be used with frac. Defaults to None.

None
frac float | None

Fraction of items to return. Cannot be used with n. Should be between 0 and 1. Defaults to None.

None
random_state int | None

Seed for the random number generator. Defaults to None.

None

Returns:

Name Type Description
LazyFrame LazyFrame

A new LazyFrame with the sampled rows.

Raises:

Type Description
ValueError

If both n and frac are specified or if neither is specified. Also if frac is not between 0 and 1.

Examples:

# Sample 2 rows from the DataFrame
df.sample(n=2)

# Sample 10% of rows from the DataFrame
df.sample(frac=0.1)

# Sample 5 rows with a fixed random seed
df.sample(n=5, random_state=42)