Skip to content

Instantly share code, notes, and snippets.

# Implementation with Pandas
import pandas as pd
# Read in the cash flows data and rate data as csv
cashflow_df = pd.read_csv(path_cashflow)
rate_df = pd.read_csv(path_rate)
# Calculate discount factor from the rates
rate_df["Discount factor"] = 1 / (1 + rate_df["Interest rate"])**rate_df["Year"]
# Implementation with Koalas
import databricks.koalas as ks
# Read in the cash flows data and rate data as csv
cashflow_df = ks.read_csv(path_cashflow)
rate_df = ks.read_csv(path_rate)
# Calculate discount factor from the rates
rate_df["Discount factor"] = 1 / (1 + rate_df["Interest rate"])**rate_df["Year"]
# Implementation with PySpark
from pyspark.sql import SparkSession
from pyspark.sql import Window
from pyspark.sql import functions as f
# Define Spark settings
builder = SparkSession.builder.appName("Discount_Cashflows")
spark = builder.getOrCreate()
# Read in the cash flows data and rate data as csv
# Groupby product and check the profitability
cf_with_rate_df = cf_with_rate_df.groupby("Product")[["Present value"]].sum().reset_index()
Year Cash flows Currency Interest rate Discount factor
0 12000 USD 0.025 1.000
1 -2000 USD 0.025 0.976
2 -2000 USD 0.025 0.952
3 -2000 USD 0.025 0.929
4 -2000 USD 0.025 0.906
5 -2000 USD 0.025 0.884
Client Insurance product Country Duration Price Payment year 1 Payment year 2 Payment year 3 Payment year 4
A Small car UK 1 200 0 50 30 0
Client Insurance product Country Duration Price Payment year 1 Payment year 2 Payment year 3 Payment year 4
A Small car UK 1 200 0 50 30 0
B Small car Italy 1 100 0 0 0 80
C Small car UK 1 200 0 35 40 20
D Small car UK 1 220 0 40 60 0
E Small car France 1 200 0 20 30 50
F Businss car Belgium 1 450 0 100 100 100
G Businss car Germany 1 470 0 120 80 0
H Businss car UK 1 400 60 70 100 0
I Businss car UK 1 400 50 70 20 80