How can I apply a custom function to each row of a Pandas dataframe df1, where:
- the function uses values from a column in
df1 - the function uses values from another dataframe
df2 - the results are appended to
df1column-wise
Example:
df1 = pd.DataFrame([1, 2, 3], columns=["x"])
df2 = pd.DataFrame({"set1": [0, 0, 0, 0], "set2": [100, 200, 300, 400]})
display(df1, df2)
And custom function
def myfunc(df2, x=df1["x"]):
# Something simple but custom
ans = df2["set1"] + df2["set2"] * x
return ans
Desired output is
| x | run1 | run2 | run3 | run4 | |
|---|---|---|---|---|---|
| 0 | 1 | 100 | 200 | 300 | 400 |
| 1 | 2 | 200 | 400 | 600 | 800 |
| 2 | 3 | 300 | 600 | 900 | 1200 |
Here is an example function call; but how can I apply it with a oneliner to get the desired dataframe output?
test = myfunc(df2,x=3)
print(test)

