Return a random sample of items from an axis of object. You can use `random_state` for reproducibility. Parameters ---------- n : int, optional Number of items from axis to return. Cannot be used with `frac`. Default = 1 if `frac` =
(
self,
n: int | None = None,
frac: float | None = None,
replace: bool = False,
weights=None,
random_state: RandomState | None = None,
axis: Axis | None = None,
ignore_index: bool = False,
)
| 5840 | |
| 5841 | @final |
| 5842 | def sample( |
| 5843 | self, |
| 5844 | n: int | None = None, |
| 5845 | frac: float | None = None, |
| 5846 | replace: bool = False, |
| 5847 | weights=None, |
| 5848 | random_state: RandomState | None = None, |
| 5849 | axis: Axis | None = None, |
| 5850 | ignore_index: bool = False, |
| 5851 | ) -> Self: |
| 5852 | """ |
| 5853 | Return a random sample of items from an axis of object. |
| 5854 | |
| 5855 | You can use `random_state` for reproducibility. |
| 5856 | |
| 5857 | Parameters |
| 5858 | ---------- |
| 5859 | n : int, optional |
| 5860 | Number of items from axis to return. Cannot be used with `frac`. |
| 5861 | Default = 1 if `frac` = None. |
| 5862 | frac : float, optional |
| 5863 | Fraction of axis items to return. Cannot be used with `n`. |
| 5864 | replace : bool, default False |
| 5865 | Allow or disallow sampling of the same row more than once. |
| 5866 | weights : str or ndarray-like, optional |
| 5867 | Default ``None`` results in equal probability weighting. |
| 5868 | If passed a Series, will align with target object on index. Index |
| 5869 | values in weights not found in sampled object will be ignored and |
| 5870 | index values in sampled object not in weights will be assigned |
| 5871 | weights of zero. |
| 5872 | If called on a DataFrame, will accept the name of a column |
| 5873 | when axis = 0. |
| 5874 | Unless weights are a Series, weights must be same length as axis |
| 5875 | being sampled. |
| 5876 | If weights do not sum to 1, they will be normalized to sum to 1. |
| 5877 | Missing values in the weights column will be treated as zero. |
| 5878 | Infinite values not allowed. |
| 5879 | When replace = False will not allow ``(n * max(weights) / sum(weights)) > 1`` |
| 5880 | in order to avoid biased results. See the Notes below for more details. |
| 5881 | random_state : int, array-like, BitGenerator, np.random.RandomState, np.random.Generator, optional |
| 5882 | If int, array-like, or BitGenerator, seed for random number generator. |
| 5883 | If np.random.RandomState or np.random.Generator, use as given. |
| 5884 | Default ``None`` results in sampling with the current state of np.random. |
| 5885 | axis : {0 or 'index', 1 or 'columns', None}, default None |
| 5886 | Axis to sample. Accepts axis number or name. Default is stat axis |
| 5887 | for given data type. For `Series` this parameter is unused and defaults to `None`. |
| 5888 | ignore_index : bool, default False |
| 5889 | If True, the resulting index will be labeled 0, 1, …, n - 1. |
| 5890 | |
| 5891 | Returns |
| 5892 | ------- |
| 5893 | Series or DataFrame |
| 5894 | A new object of same type as caller containing `n` items randomly |
| 5895 | sampled from the caller object. |
| 5896 | |
| 5897 | See Also |
| 5898 | -------- |
| 5899 | DataFrameGroupBy.sample: Generates random samples from each group of a |