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

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]