I'd like to iterate row-wise over 2 identically-shaped dataframes, passing the rows from each as vectors to a function without using loops. Essentially something similar to R's mapply.
I've investigated a little and the best that I've seen uses map in a list comprehension, but I'm not doing it correctly. Even if we get this to work, though, it seems a bit clunky - is there a more elegant way to do this? Seems like this should be a functionality in pandas.
import numpy as np
import pandas as pd
from scipy import stats
df1 = pd.DataFrame(np.random.randn(3,3))
df2 = pd.DataFrame(np.random.randn(3,3))
sd_array = np.array([0.02, 0.015, 0.2])
def helper_func(x, y):
return stats.norm.pdf(x, loc=y, scale=sd_array).prod()
res_lst = []
row_cnt = df1.shape[0]
res = [list(map(helper_func, df1.iloc[i,:], df2.iloc[i,:])) for i in range(row_cnt)]
res_lst.append(res)
The way I currently have it written doesn't give an error but also doesn't return what I want. I should only have 3 values in the output, one for each row of the dataframe.