Vectorized operations on a pd.Series

A vectorized operation on one Series

Inflict 6 percent inflation.

import sys
import numpy as np
import pandas as pd

data = np.arange(10.00, 60.00, 10.00)
index = pd.RangeIndex(5, name = "day")
series = pd.Series(data = data, index = index, name = "prices")
print(series)
print()

#Hard way.
#for i in range(len(series)):
#    series[i] *= 1.06 #Means series[i] = series[i] * 1.06

series *= 1.06         #Easy way.  Means series = series * 1.06
print(series)          #Means print(series.to_string())
print()

#Print each price in a format that looks like money.
s = series.to_string(dtype = True, float_format = lambda price: f"${price:.2f}", name = True)
print(s)
print()

sys.exit(0)

If you don’t like lambda functions, you can say

def formatter(price):
    return f"${price:.2f}"

s = series.to_string(dtype = True, float_format = formatter, name = True)
day
0    10.0
1    20.0
2    30.0
3    40.0
4    50.0
Name: prices, dtype: float64

day
0    10.6
1    21.2
2    31.8
3    42.4
4    53.0
Name: prices, dtype: float64

day
0   $10.60
1   $21.20
2   $31.80
3   $42.40
4   $53.00
Name: prices, dtype: float64

Convert Fahrenheit to Celsius.

"Convert a Fahrenheit Series to Celsius."

import sys
import pandas as pd

data = [
    -459.67, #absolute zero
     -40.0,  #same in Fahrenheit and Celsius
      32.0,  #freezing
      98.6,  #body temperature
     212.0   #boiling
]

series = pd.Series(data = data, name = "temperature")   #Fahrenheit
print(series)
print()

#Create and print a new Series.  Then discard it.

print((series - 32) * 5/9)  #Convert Fahrenheit to Celsius.
print()

#Change the values in the original Series.

series -= 32    #means series = series - 32
series *= 5/9   #means series = series * 5/9
print(series)

sys.exit(0)
0   -459.67
1    -40.00
2     32.00
3     98.60
4    212.00
Name: temperature, dtype: float64

0   -273.15
1    -40.00
2      0.00
3     37.00
4    100.00
Name: temperature, dtype: float64

0   -273.15
1    -40.00
2      0.00
3     37.00
4    100.00
Name: temperature, dtype: float64

The map method creates a new Series.

"Create a Series of strings from a Series of floats."

import sys
import numpy as np
import pandas as pd

data = np.arange(10.00, 60.00, 10.00)
index = pd.RangeIndex(5, name = "day")
series = pd.Series(data = data, index = index, name = "prices")

series *= 1.06   #means series = series * 1.06
print(series)
print()

seriesOfStrings = series.map(lambda price: f"${price:.2f}")
print(seriesOfStrings)

#or just say print(series.map(lambda price: f"${price:.2f}"))

sys.exit(0)
day
0    10.6
1    21.2
2    31.8
3    42.4
4    53.0
Name: prices, dtype: float64

day
0    $10.60
1    $21.20
2    $31.80
3    $42.40
4    $53.00
Name: prices, dtype: object
"Create a Series of Celsius temperatures from a Series of Fahrenheit temperatures."

import sys
import pandas as pd

data = [
    -459.67, #absolute zero
     -40.0,  #same in Fahrenheit and Celsius
      32.0,  #freezing
      98.6,  #body temperature
     212.0   #boiling
]

series = pd.Series(data = data, name = "temperature")   #Fahrenheit

#Create and print a new Series.

def fahrenheitToCelsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

seriesOfCelsius = series.map(fahrenheitToCelsius)
print(seriesOfCelsius)

#or just say print(series.map(lambda fahrenheit: (fahrenheit - 32) * 5/9))

sys.exit(0)
0   -273.15
1    -40.00
2      0.00
3     37.00
4    100.00
Name: temperature, dtype: float64

Use map to left-justify a Series of strings

import sys
import pandas as pd

series = pd.Series(data = ["New York", "New Jersey", "Connecticut"])
print(series)
print()

maxlen = series.map(len).max()
series = series.map(lambda s: f"{s:{maxlen}}")
print(series)

sys.exit(0)
0       New York
1     New Jersey
2    Connecticut
dtype: object

0    New York
1    New Jersey
2    Connecticut
dtype: object

A vectorized operation on two Serieses

"Add two Serieses to create a third Series."

import sys
import numpy as np
import pandas as pd

data = [10.00, 20.00, 30.00, 40.00, 50.00]
prices = pd.Series(data = data, name = "prices")
prices.index.name = "day"

data = [1.00, 2.00, 3.00, 4.00, 5.00]
taxes = pd.Series(data = data, name = "taxes")
taxes.index.name = "day"

totalPrices = prices + taxes
totalPrices.name = "total prices"
print(totalPrices)
print()

totalPrices = prices.add(taxes)   #Does the same thing.
totalPrices.name = "total prices"
print(totalPrices)

sys.exit(0)
day
0    11.0
1    22.0
2    33.0
3    44.0
4    55.0
Name: total prices, dtype: float64

day
0    11.0
1    22.0
2    33.0
3    44.0
4    55.0
Name: total prices, dtype: float64
"""
Add two Serieses to create a third Series.
One of the original Serieses contains a np.nan.
"""

import sys
import numpy as np
import pandas as pd

data = [10.00, 20.00, 30.00, 40.00, 50.00]
prices = pd.Series(data = data, name = "prices")
prices.index.name = "day"

data = [1.00, 2.00, 3.00, np.nan, 5.00]
taxes = pd.Series(data = data, name = "taxes")
taxes.index.name = "day"

totalPrices = prices + taxes
totalPrices.name = "total prices"
print(totalPrices)
print()

totalPrices = prices.add(taxes, fill_value = 0.00)
totalPrices.name = "total prices"
print(totalPrices)

sys.exit(0)
day
0    11.0
1    22.0
2    33.0
3     NaN
4    55.0
Name: total prices, dtype: float64

day
0    11.0
1    22.0
2    33.0
3    40.0
4    55.0
Name: total prices, dtype: float64
"""
Add two Serieses to create a third Series.
Drop every row that has a np.nan.
"""

import sys
import numpy as np
import pandas as pd

data = [10.00, 20.00, 30.00, 40.00, 50.00]
prices = pd.Series(data = data, name = "prices")
prices.index.name = "day"

data = [1.00, 2.00, 3.00, np.nan, 5.00]
taxes = pd.Series(data = data, name = "taxes")
taxes.index.name = "day"

totalPrices = prices + taxes
totalPrices.name = "total prices"
print(totalPrices)
print()

totalPrices.dropna(inplace = True)   #or totalPrices = totalPrices.dropna()
print(totalPrices)

sys.exit(0)
day
0    11.0
1    22.0
2    33.0
3     NaN
4    55.0
Name: total prices, dtype: float64

day
0    11.0
1    22.0
2    33.0
4    55.0
Name: total prices, dtype: float64