Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas
Series.sample() function return a random sample of items from an axis of object. We can also use random_state for reproducibility.
Syntax: Series.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)
Parameter :
n : Number of items from axis to return.
frac : Fraction of axis items to return.
replace : Sample with or without replacement.
weights : Default βNoneβ results in equal probability weighting.
random_state : Seed for the random number generator (if int), or numpy RandomState object.
axis : Axis to sample.
Returns : Series or DataFrame
Example #1: Use
Series.sample() function to draw random sample of the values from the given Series object.
Output :
π Image
Now we will use
Series.sample() function to draw a random sample of values from the given Series object.
Output :
π Image
As we can see in the output, the
Series.sample() function has successfully returned a random sample of 3 values from the given Series object.
Example #2: Use
Series.sample() function to draw random sample of the values from the given Series object.
Output :
π Image
Now we will use
Series.sample() function to select a random sample of size equivalent to 25% of the size of the given Series object.
Output :
π Image
As we can see in the output, the
Series.sample() function has successfully returned a random sample of 2 values from the given Series object, which is 25% of the size of the original series object.