Series.sample()
Pandas Series.sample()
returns a random sample of items from the object axis. 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 ()
to draw a random sample of values from a given Series object.
< p> # import pandas as pd

Output:
Now we will use Series.sample ()
to draw a random sample of values from a given object that Series.

Output:
As we can see in the output, Series.sample ()
successfully returned a random sample of 3 values from the given Series object.
Example # 2: Use Series.sample ()
to draw a random you a collection of values from this Series object.

Output:
Now we will use Call Series.sample ()
to select a random sample equivalent to 25% of the size of this Series object.
# Draw a random sample size 25% of the original object
selected_items
=
sr.sample (frac
=
0.25
)
# Print the returned Series object
print
(selected_items)
Output:
As we can see in the output, Series.sample ()
successfully returned a random selection 2 values from this Series object, which is 25% of the size of the original series object.
X
Submit new EBook