Using pandas and numpy, what is the most efficient way to do what the f1
function does?
import numpy as np
import pandas as pd
from time import time
n = 10000
df = pd.DataFrame()
df["a"] = np.random.randn(n)
df["b"] = np.random.uniform(n)
def f1(df):
df.loc[0, "c"] = 100
for i in range(1, len(df)):
df.loc[i, "c"] = df.loc[i, "a"] * df.loc[i, "b"] +\
(1 - df.loc[i, "a"]) * df.loc[i - 1, "c"]
start_time = time()
f1(df)
ellapsed_time = time() - start_time
print(ellapsed_time)