- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.sample#
- DataFrame.sample(n=None,frac=None,replace=False,weights=None,random_state=None,axis=None,ignore_index=False)[source]#
Return a random sample of items from an axis of object.
You can userandom_state for reproducibility.
- Parameters:
- nint, optional
Number of items from axis to return. Cannot be used withfrac.Default = 1 iffrac = None.
- fracfloat, optional
Fraction of axis items to return. Cannot be used withn.
- replacebool, default False
Allow or disallow sampling of the same row more than once.
- weightsstr or ndarray-like, optional
Default ‘None’ results in equal probability weighting.If passed a Series, will align with target object on index. Indexvalues in weights not found in sampled object will be ignored andindex values in sampled object not in weights will be assignedweights of zero.If called on a DataFrame, will accept the name of a columnwhen axis = 0.Unless weights are a Series, weights must be same length as axisbeing sampled.If weights do not sum to 1, they will be normalized to sum to 1.Missing values in the weights column will be treated as zero.Infinite values not allowed.
- random_stateint, array-like, BitGenerator, np.random.RandomState, np.random.Generator, optional
If int, array-like, or BitGenerator, seed for random number generator.If np.random.RandomState or np.random.Generator, use as given.
Changed in version 1.4.0:np.random.Generator objects now accepted
- axis{0 or ‘index’, 1 or ‘columns’, None}, default None
Axis to sample. Accepts axis number or name. Default is stat axisfor given data type. ForSeries this parameter is unused and defaults toNone.
- ignore_indexbool, default False
If True, the resulting index will be labeled 0, 1, …, n - 1.
Added in version 1.3.0.
- Returns:
- Series or DataFrame
A new object of same type as caller containingn items randomlysampled from the caller object.
See also
DataFrameGroupBy.sample
Generates random samples from each group of a DataFrame object.
SeriesGroupBy.sample
Generates random samples from each group of a Series object.
numpy.random.choice
Generates a random sample from a given 1-D numpy array.
Notes
Iffrac > 1,replacement should be set toTrue.
Examples
>>>df=pd.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_seenfalcon 2 2 10dog 4 0 2spider 8 0 1fish 0 0 8
Extract 3 random elements from the
Series
df['num_legs']
:Note that we userandom_state to ensure the reproducibility ofthe examples.>>>df['num_legs'].sample(n=3,random_state=1)fish 0spider 8falcon 2Name: num_legs, dtype: int64
A random 50% sample of the
DataFrame
with replacement:>>>df.sample(frac=0.5,replace=True,random_state=1) num_legs num_wings num_specimen_seendog 4 0 2fish 0 0 8
An upsample sample of the
DataFrame
with replacement:Note thatreplace parameter has to beTrue forfrac parameter > 1.>>>df.sample(frac=2,replace=True,random_state=1) num_legs num_wings num_specimen_seendog 4 0 2fish 0 0 8falcon 2 2 10falcon 2 2 10fish 0 0 8dog 4 0 2fish 0 0 8dog 4 0 2
Using a DataFrame column as weights. Rows with larger value in thenum_specimen_seen column are more likely to be sampled.
>>>df.sample(n=2,weights='num_specimen_seen',random_state=1) num_legs num_wings num_specimen_seenfalcon 2 2 10fish 0 0 8