Change language

Python | Pandas Series.sample ()

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

import pandas as pd

 
# Create a series

sr = pd.Series ([ ’New York’ , ’ Chicago’ , ’Toronto’ , ’ Lisbon’ , ’Rio’ , ’ Moscow’ ])

 
# Create date index and time

index_ = [ ’City 1’ , ’ City 2’ , ’City 3’ , ’City 4’ , ’ City 5’ , ’City 6’ ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

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

# Draw a random sample of 3 values ​​

selected_cities = sr.sample (n = 3 )

  
# Print the returned Series object

print (selected_cities)

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.

# import pandas as pd

import pandas as pd

 
# Create Series

sr = pd .Series ([ 100 , 25 , 32 , 118 , 24 , 65 ])

 
# Create index

index_ = [ ’ Coca Cola’ , ’Sprite’ , ’ Coke’ , ’Fanta’ , ’ Dew’ , ’ThumbsUp’ ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

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.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically