4.10. Series Alter

4.10.1. SetUp

>>> import pandas as pd
>>>
>>> s = pd.Series([1.0, 2.0, 2.0, None, 5.0])

4.10.2. Drop Rows

  • Series.drop() removes rows by index

  • Drop element at index

  • Argument can be an int or a list[int]

>>> s.drop(1)
0    1.0
2    2.0
3    NaN
4    5.0
dtype: float64
>>> s.drop([0,2,4])
1    2.0
3    NaN
dtype: float64

4.10.3. Drop Duplicates

  • Series.drop_duplicates() removes duplicates

>>> s.drop_duplicates()
0    1.0
1    2.0
3    NaN
4    5.0
dtype: float64

4.10.4. Reset Index

  • Series.reset_index()

  • drop=True prevents the old index being added as a column

>>> s.reset_index()
   index    0
0      0  1.0
1      1  2.0
2      2  2.0
3      3  NaN
4      4  5.0
>>> s.reset_index(drop=True)
0    1.0
1    2.0
2    2.0
3    NaN
4    5.0
dtype: float64

4.10.5. Assignments

# %% 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

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

# %% About
# - Name: Series Alter DropOne
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result: pd.Series` with result of:
#    - from `DATA` drop value at index 3
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.Series` z wynikiem:
#    - z `DATA` usuń wartość na indeksie 3
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0       Mark
1    Melissa
2       Rick
4       Beth
5      Chris
dtype: object
"""

import pandas as pd


DATA = pd.Series([
    'Mark',
    'Melissa',
    'Rick',
    'Alex',
    'Beth',
    'Chris',
])

# From `DATA` drop value at index 3
# type: pd.Series
result = ...


# %% 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

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

# %% About
# - Name: Series Alter DropMany
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result: pd.Series` with result of:
#    - from `DATA` drop values at index 1, 3, 5
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.Series` z wynikiem:
#    - z `DATA` usuń wartości na indeksach 1, 3, 5
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0    Mark
2    Rick
4    Beth
dtype: object
"""

import pandas as pd


DATA = pd.Series([
    'Mark',
    'Melissa',
    'Rick',
    'Alex',
    'Beth',
    'Chris',
])

# From `DATA` drop values at index 1, 3, 5
# type: pd.Series
result = ...


# %% 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

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

# %% About
# - Name: Series Alter DropDuplicates
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result: pd.Series` with result of:
#    - from `DATA` drop duplicates
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.Series` z wynikiem:
#    - z `DATA` usuń duplikujące się wartości
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0       Mark
1    Melissa
2       Rick
3       Alex
6       Beth
8      Chris
dtype: object
"""

import pandas as pd


DATA = pd.Series([
    'Mark',
    'Melissa',
    'Rick',
    'Alex',
    'Melissa',
    'Melissa',
    'Beth',
    'Mark',
    'Chris',
])

# From `DATA` drop duplicates
# type: pd.Series
result = ...


# %% 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

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

# %% About
# - Name: Series Alter ResetIndex
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result: pd.Series` with result of:
#    - from `DATA` reset index (without old copy)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.Series` z wynikiem:
#    - z `DATA` zresetuj indeks (bez kopii starego)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
0       Mark
1    Melissa
2       Rick
3       Alex
4       Beth
5      Chris
dtype: object
"""

import pandas as pd


DATA = pd.Series([
    'Mark',
    'Melissa',
    'Rick',
    'Alex',
    'Melissa',
    'Melissa',
    'Beth',
    'Mark',
    'Chris',
]).drop_duplicates()

# From `DATA` drop duplicates
# type: pd.Series
result = ...