3.8. Series Alter

3.8.1. Drop Rows

  • Drop element at index

  • Works with inplace=True

import pandas as pd

s = pd.Series([1.0, 2.0, 3.0, None, 5.0])

s.drop(1)
# 0    1.0
# 2    3.0
# 3    NaN
# 4    5.0
# dtype: float64

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

3.8.2. Drop Duplicates

  • Works with inplace=True

import pandas as pd

s = pd.Series([1.0, 2.0, 2.0, None, 5.0])

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

3.8.3. Reset Index

  • Works with inplace=True

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

import pandas as pd

s = pd.Series([1.0, 2.0, 3.0, None, 5.0])

s.drop([0,1], inplace=True)
# 2    3.0
# 3    NaN
# 4    5.0
# dtype: float64

s.reset_index()
#    index    0
# 0      2  3.0
# 1      3  NaN
# 2      4  5.0

s.reset_index(drop=True, inplace=True)
# 0    3.0
# 1    NaN
# 2    5.0
# dtype: float64

3.8.4. Assignments

Code 3.41. 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

    >>> type(result) is pd.Series
    True
    >>> result
    0    1.0
    1    NaN
    2    2.0
    dtype: float64
"""

import pandas as pd

DATA = [1, None, 5, None, 1, 2, 1]

result = ...