5.11. Series Alter

5.11.1. Drop

  • Drop element at index

  • Can use with inplace=True

import pandas as pd

data = [1, np.nan, 5, np.nan, 1, 2, 1, np.inf]
s = pd.Series(data)

s.drop(1)
# 0    1.0
# 2    5.0
# 3    NaN
# 4    1.0
# 5    2.0
# 6    1.0
# 7    inf
# dtype: float64

5.11.2. Drop duplicates

  • Modifies inplace

import pandas as pd

data = [1, np.nan, 5, np.nan, 1, 2, 1, np.inf]
s = pd.Series(data)

s.drop_duplicates()
# 0    1.0
# 1    NaN
# 2    5.0
# 5    2.0
# 7    inf
# dtype: float64

5.11.3. Drop NaN

  • can use with inplace=True

import pandas as pd

data = [1, np.nan, 5, np.nan, 1, 2, 1, np.inf]
s = pd.Series(data)

s.dropna()
# 0    1.0
# 2    5.0
# 4    1.0
# 5    2.0
# 6    1.0
# 7    inf
# dtype: float64

5.11.4. Reset Index

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

import pandas as pd

data = [1, np.nan, 5, np.nan, 1, 2, 1, np.inf]
s = pd.Series(data)
s = s.drop_duplicates()
# 0    1.0
# 1    NaN
# 2    5.0
# 5    2.0
# 7    inf
# dtype: float64

s.reset_index()
#    index    0
# 0      0  1.0
# 1      1  NaN
# 2      2  5.0
# 3      5  2.0
# 4      7  inf

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

5.11.5. Assignments

5.11.5.1. Update

  • Complexity level: easy

  • Lines of code to write: 10 lines

  • Estimated time of completion: 5 min

  • Filename: solution/series_update.py

English
  1. From input data create pd.Series

  2. Fill empty values with zero

  3. Drop values at index 2, 4, 6

  4. Drop duplicates

  5. Reindex series (without old copy)

  6. Print series

Polish
  1. Z danych wejściowych stwórz pd.Series

  2. Wypełnij puste wartości zerami

  3. Usuń wartości na indeksach 2, 4, 6

  4. Usuń duplikujące się wartości

  5. Zresetuj indeks (bez kopii starego)

  6. Wypisz serię

Input
[1, np.nan, 5, np.nan, 1, 2, 1, np.inf]