Skip to content
Advertisement

Iterating row-wise over 2 pandas dataframes and passing these vectors as args to function

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.

JavaScript

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.

Advertisement

Answer

You can just do helper_func(df1, df2), and in helper_func: return stats.norm.pdf(x, loc=y, scale=sd_array).prod(axis=1). Be aware that your scale is such, that the values returned are almost always 0. Using scale=100*sd_array in the PDF will at least show some non-zero values.

In fact, you don’t need a dataframe in this example:

JavaScript

yields

JavaScript

But when using a dataframe instead of data1 or data2, NumPy/Pandas/Scipy are flexible enough to recognize the 2D array of values and use it as such.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement