3.8. Series Alter

3.8.1. SetUp

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

3.8.2. Drop Rows

  • Drop element at index

  • Works with inplace=True

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

3.8.3. Drop Duplicates

  • Works with inplace=True

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

3.8.4. Reset Index

  • Works with inplace=True

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

>>> s.drop([0,1], inplace=True)
>>> s
2    2.0
3    NaN
4    5.0
dtype: float64
>>> 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

3.8.5. Assignments

Code 3.68. Solution
"""
* Assignment: Series Alter
* Complexity: easy
* Lines of code: 10 lines
* Time: 5 min

English:
    1. From input data create `s: pd.Series`
    2. Drop values at index 2, 4, 6
    3. Drop duplicates
    4. Reindex series (without old copy)
    5. Run doctests - all must succeed

Polish:
    1. Z danych wejściowych stwórz `s: pd.Series`
    2. Usuń wartości na indeksach 2, 4, 6
    3. Usuń duplikujące się wartości
    4. Zresetuj indeks (bez kopii starego)
    5. Uruchom doctesty - wszystkie muszą się powieść

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 = [1, None, 5, None, 1, 2, 1]

# type: pd.Series
result = ...