5.11. Series Arithmetic

5.11.1. Vectorized Operations

5.11.1.1. Scalar Arithmetic

import pandas as pd
import numpy as np

s = pd.Series(
    data = [1.0, 2.0, 3.0, np.nan, 5.0],
    index = ['a', 'b', 'c', 'd', 'e'])

s
# a    1.0
# b    2.0
# c    3.0
# d    NaN
# e    5.0
# dtype: float64
s * 5
# a     5.0
# b    10.0
# c    15.0
# d     NaN
# e    25.0
# dtype: float64
s ** 2
# a    1.0
# b    4.0
# c    9.0
# d    NaN
# e    25.0
# dtype: float64
s ** 3
# a     1.0
# b     8.0
# c    27.0
# d     NaN
# e   125.0
# dtype: float64

5.11.2. Series Arithmetic

  • Uses inner join

  • fill_value: If data in both corresponding Series locations is missing the result will be missing

import pandas as pd
import numpy as np

a = pd.Series(
    data = [1.0, 2.0, 3.0, np.nan],
    index = ['a', 'b', 'c', 'd'])

a
# a    1.0
# b    2.0
# c    3.0
# d    NaN
# dtype: float64

b = pd.Series(
    data = [10.0, np.nan, 12.0, np.nan],
    index = ['a', 'b', 'x', 'y'])

b
# a    10.0
# b    NaN
# x    12.0
# y    NaN
# dtype: float64
a + b
# a    11.0
# b    NaN
# c    NaN
# d    NaN
# x    NaN
# y    NaN
# dtype: float64
Listing 684. fill_value: If data in both corresponding Series locations is missing the result will be missing
a.add(b, fill_value=0)
# a    11.0
# b     2.0
# c     3.0
# d     NaN
# x    12.0
# y     NaN
# dtype: float64

5.11.3. Assignments

5.11.3.1. Arithmetic

English
  1. Set random seed to zero

  2. Generate data: ndarray with 5 random digits [0, 9]

  3. Create index: list with index names as sequential letters in english alphabet

  4. Create s: pd.Series from data and index

  5. Multiply s by 10

  6. Multiply s by original s values (before multiplying by 10)

Polish
  1. Ustaw random seed na zero

  2. Wygeneruj data: ndarray z 5 losowymi cyframi <0, 9>

  3. Stwórz index: list z indeksami jak kolejne listery alfabetu angielskiego

  4. Stwórz s: pd.Series z data oraz index

  5. Pomnóż s przez 10

  6. Pomnóż s przez oryginalne wartości s (przed mnożeniem przez 10)