0

I have a function (hypothetical):

def example_fct(x,y)
    return x*y

I want to apply this function into a hypothetical dataframe:

df = 
   number1   number2
0    20        30
1    25        10

Where it will result as:

   number1   number2   multiply
0    20        30        600
1    25        10        250

I tried to use apply:

df_['multiply'] = example_fct(df.number1,df.number2)

But this does not work as the function arguments are scalar instead of series. I can always use .apply for a function that has single input argument, but this function uses 2 input arguments.

Furthermore, I am also wondering if this function can be used with series from different data frames (but both data frames have the same length).

1
  • df.number1.mul(df.number2) or np.multiply(df.number1, df.number2) Commented Jul 28, 2020 at 10:36

1 Answer 1

2
In [81]: df
Out[81]:
   number1  number2
0       20       30
1       25       10

In [82]: def example_fct(x,y):
    ...:     return x*y
    ...:

In [83]: df["multiply"] = df.apply(lambda x:example_fct(x["number1"], x["number2"]), axis=1)

In [84]: df
Out[84]:
   number1  number2  multiply
0       20       30       600
1       25       10       250
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.