5.10. Series Alter

5.10.1. Drop

  • Drop element at index

  • Works with inplace=True

import pandas as pd
import numpy as np

s = pd.Series([1.0, 2.0, 3.0, np.nan, 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

5.10.2. Drop duplicates

  • Works with inplace=True

import pandas as pd
import numpy as np

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

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

5.10.3. Drop NaN

  • Works with inplace=True

import pandas as pd
import numpy as np

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

s.dropna()
# 0    1.0
# 1    2.0
# 2    2.0
# 4    5.0
# dtype: float64

5.10.4. Reset Index

  • Works with inplace=True

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

import pandas as pd
import numpy as np

s = pd.Series([1.0, 2.0, 3.0, np.nan, 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)
# 0    3.0
# 1    NaN
# 2    5.0
# dtype: float64

5.10.5. Assignments

5.10.5.1. Series Update

  • Complexity level: easy

  • Lines of code to write: 10 lines

  • Estimated time of completion: 5 min

  • Solution: solution/series_update.py

English
  1. Use data from "Input" section (see below)

  2. From input data create pd.Series

  3. Fill empty values with zero

  4. Drop values at index 2, 4, 6

  5. Drop duplicates

  6. Reindex series (without old copy)

  7. Print series

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Z danych wejściowych stwórz pd.Series

  3. Wypełnij puste wartości zerami

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

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

  6. Zresetuj indeks (bez kopii starego)

  7. Wypisz serię

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