snowflake.snowpark.DataFrame.sample

DataFrame.sample(frac: Optional[float] = None, n: Optional[int] = None) → DataFrame[source]

Returns a random sample of rows using Snowflake’s SQL SAMPLE clause.

Specify either frac or n. Fractional sampling includes each row with the given probability, so the number of returned rows can vary. Fixed-size sampling returns the requested number of rows, or all rows if the input contains fewer rows. Neither form guarantees row order. Repeated executions can return different samples; this method has no seed parameter. For seeded sampling of a table, see Table.sample().

See SAMPLE for SQL sampling semantics.

Parameters:
  • frac – The probability of selecting each row, from 0.0 to 1.0 inclusive. For example, 0.1 requests approximately 10 percent of the rows, not exactly 10 percent.

  • n – the number of rows to sample in the range of 0 to 1,000,000 (inclusive).

Returns:

a DataFrame containing the sample of rows.

Examples:

>>> df = session.range(100)
>>> fractional_sample = df.sample(frac=0.1)
>>> fixed_sample = df.sample(n=5)
>>> assert fixed_sample.count() == 5