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

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

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

4.10.5. Assignments

Code 4.79. Solution
"""
* Assignment: Series Alter DropOne
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

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

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

Run:
    * PyCharm: right-click in the editor and pick `Run Doctest in myfile`
    * PyCharm: `Control + Shift + R`
    * Terminal: `python -m doctest -v myfile.py`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> 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 = ...


Code 4.80. Solution
"""
* Assignment: Series Alter DropMany
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

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

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

Run:
    * PyCharm: right-click in the editor and pick `Run Doctest in myfile`
    * PyCharm: `Control + Shift + R`
    * Terminal: `python -m doctest -v myfile.py`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> 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 = ...


Code 4.81. Solution
"""
* Assignment: Series Alter DropDuplicates
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

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

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

Run:
    * PyCharm: right-click in the editor and pick `Run Doctest in myfile`
    * PyCharm: `Control + Shift + R`
    * Terminal: `python -m doctest -v myfile.py`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> 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    1.0
    1    NaN
    2    2.0
    dtype: float64
"""

import pandas as pd


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

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


Code 4.82. Solution
"""
* Assignment: Series Alter ResetIndex
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

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

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

Run:
    * PyCharm: right-click in the editor and pick `Run Doctest in myfile`
    * PyCharm: `Control + Shift + R`
    * Terminal: `python -m doctest -v myfile.py`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> 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 = ...