5.6. Series Select

5.6.1. SetUp

>>> import pandas as pd
>>> import numpy as np
>>>
>>> data = ['Alice', 'Bob', 'Carol']
>>> s = pd.Series(data)
>>>
>>> s
0    Alice
1      Bob
2    Carol
dtype: str

5.6.3. Tail

>>> s.tail(2)
1      Bob
2    Carol
dtype: str
>>> s.tail(n=2)
1      Bob
2    Carol
dtype: str

5.6.4. Sample

>>> s.sample(2)
2    Carol
1      Bob
dtype: str
>>> s.sample(n=2)
2    Carol
1      Bob
dtype: str

5.6.5. Sample Fraction

>>> s.sample(frac=0.5)
2    Carol
1      Bob
dtype: str
>>> s.sample(frac=1/2)
2    Carol
1      Bob
dtype: str

5.6.6. Sample With Replacement

n number or fraction random rows with and without repetition:

>>> s.sample(frac=0.5, replace=True)
0    Alice
1      Bob
dtype: str

5.6.7. Reset Index

>>> s.sample(frac=0.5)
2    Carol
1      Bob
dtype: str
>>> s.sample(frac=0.5).reset_index()
   index      0
0      2  Carol
1      1    Bob
>>> s.sample(frac=0.5).reset_index(drop=True)
0    Carol
1      Bob
dtype: str

5.6.8. Assignments

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with first element from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z pierwszym elementem z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-01    1.7641
# Freq: D, dtype: float64

# %% Hints
# - `pd.Series.head(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-01    1.7641
Freq: D, dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with last five elements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z pięcioma ostatnimi elementami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-07    0.9501
# 2000-01-08   -0.1514
# 2000-01-09   -0.1032
# 2000-01-10    0.4106
# Freq: D, dtype: float64

# %% Hints
# - `pd.Series.tail(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-07    0.9501
2000-01-08   -0.1514
2000-01-09   -0.1032
2000-01-10    0.4106
Freq: D, dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with three random elements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z trzema losowymi elementami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-03    0.9787
# 2000-01-04    2.2409
# dtype: float64

# %% Hints
# - `pd.Series.sample(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> import pandas

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-03    0.9787
2000-01-04    2.2409
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with 100% of random elements without replacements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z 100% losowych elementów bez powtórzeń z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-03    0.9787
# 2000-01-04    2.2409
# 2000-01-05    1.8676
# 2000-01-02    0.4002
# 2000-01-01    1.7641
# 2000-01-10    0.4106
# 2000-01-09   -0.1032
# 2000-01-08   -0.1514
# 2000-01-07    0.9501
# dtype: float64

# %% Hints
# - `Series.sample(frac)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-03    0.9787
2000-01-04    2.2409
2000-01-05    1.8676
2000-01-02    0.4002
2000-01-01    1.7641
2000-01-10    0.4106
2000-01-09   -0.1032
2000-01-08   -0.1514
2000-01-07    0.9501
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with 125% of random elements with replacement from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z 125% losowych elementów z powtórzeniami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-07    0.9501
# 2000-01-08   -0.1514
# 2000-01-08   -0.1514
# 2000-01-09   -0.1032
# 2000-01-02    0.4002
# 2000-01-06   -0.9773
# 2000-01-10    0.4106
# 2000-01-09   -0.1032
# 2000-01-10    0.4106
# 2000-01-05    1.8676
# 2000-01-04    2.2409
# 2000-01-01    1.7641
# dtype: float64

# %% Hints
# - `Series.sample(frac, replace)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-07    0.9501
2000-01-08   -0.1514
2000-01-08   -0.1514
2000-01-09   -0.1032
2000-01-02    0.4002
2000-01-06   -0.9773
2000-01-10    0.4106
2000-01-09   -0.1032
2000-01-10    0.4106
2000-01-05    1.8676
2000-01-04    2.2409
2000-01-01    1.7641
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...