bpd.DataFrame.sample

DataFrame.sample(n=None, *, replace=False, random_state=None)[source]

Return a random sample of rows from a data frame.

You can use random_state for reproducibility.

Parameters:
  • n (None or int, optional) – Number of rows to return. None corresponds to 1.
  • replace ({False, True}, optional, keyword only.) – Sample with or without replacement.
  • random_state (int or numpy.random.RandomState, optional, keyword only) – Seed for the random number generator (if int), or numpy RandomState object.
Returns:

s_df – A new DataFrame containing n items randomly sampled from the caller object.

Return type:

DataFrame

Raises:

ValueError – If a sample larger than the length of the DataFrame is taken without replacement.

Examples

>>> df = bpd.DataFrame().assign(letter=['a', 'b', 'c'],
...                             count=[9, 3, 3],
...                             points=[1, 2, 2])
>>> df.sample(1, random_state=0)
    letter  count  points
2      c      3       2