bigframes.pandas.Series.sample#
- Series.sample(n: int | None = None, frac: float | None = None, *, random_state: int | None = None, sort: bool | Literal['random'] | None = 'random') Series[source]#
Return a random sample of items from an axis of object.
You can use random_state for reproducibility.
Examples:
>>> import bigframes.pandas as bpd >>> df = bpd.DataFrame({'num_legs': [2, 4, 8, 0], ... 'num_wings': [2, 0, 0, 0], ... 'num_specimen_seen': [10, 2, 1, 8]}, ... index=['falcon', 'dog', 'spider', 'fish']) >>> df num_legs num_wings num_specimen_seen falcon 2 2 10 dog 4 0 2 spider 8 0 1 fish 0 0 8 [4 rows x 3 columns]
Fetch one random row from the DataFrame (Note that we use random_state to ensure reproducibility of the examples):
>>> df.sample(random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 [1 rows x 3 columns]
A random 50% sample of the DataFrame:
>>> df.sample(frac=0.5, random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 [2 rows x 3 columns]
Extract 3 random elements from the Series df[‘num_legs’]:
>>> s = df['num_legs'] >>> s.sample(n=3, random_state=1) dog 4 fish 0 spider 8 Name: num_legs, dtype: Int64
- Parameters:
n (Optional[int], default None) – Number of items from axis to return. Cannot be used with frac. Default = 1 if frac = None.
frac (Optional[float], default None) – Fraction of axis items to return. Cannot be used with n.
random_state (Optional[int], default None) – Seed for random number generator.
sort (Optional[bool|Literal["random"]], default "random") –
‘random’ (default): No specific ordering will be applied after sampling.
’True’ : Index columns will determine the sample’s order.
’False’: The sample will retain the original object’s order.
- Returns:
A new object of same type as caller containing n items randomly sampled from the caller object.
- Return type:
- Raises:
ValueError – If both
nandfracare specified.