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

3.8.4.1. Series Alter

  • Assignment: Series Alter

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 10 lines

  • Estimated time of completion: 5 min

  • Filename: solution/series_alter.py

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

  2. From input data create pd.Series

  3. Drop values at index 2, 4, 6

  4. Drop duplicates

  5. Reindex series (without old copy)

  6. Print series

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

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

  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ę

Given:
DATA = [1, None, 5, None, 1, 2, 1]
Tests:
s: pd.Series
# 0    1.0
# 1    NaN
# 2    2.0
# dtype: float64