0

I am sorry for asking such a trivial question, but I keep making mistakes when using the apply function with a lambda function that has input parameters.

See below:

df = pd.DataFrame([["John",1,3],["James",2,3],
            ["Femi",3,4], ["Rita",3,3],
            ["Rita",3,3]], columns=["Name","Age","Height"])


%timeit df["product_AH"] = df[["Age", "Height"]].apply(lambda x,y: x['Age']*y['Height'], axis=1)

Expected output:

    Name    Age  Height  product_AH
0   John    1     3          3
1   James   2     3          6
2   Femi    3     4          12
3   Rita    3     3          9
4   Rita    3     3          9
5
  • 1
    this link explains how to use the args in the apply function. For the use case above, df.Age*df.Height should suffice; u dont need apply for that. Commented Apr 4, 2020 at 12:34
  • 1
    df[["Age", "Height"]].apply(lambda x: x['Age']*x['Height'], axis=1) , you dont need 2 args here , though you dont need apply at all , pandas is vectorized to do this Commented Apr 4, 2020 at 12:35
  • 2
    The function passed to apply will recieve a single argument which will a series representing each row, in this case. But apply here is not what you want to use, you want df['Age'] * df['Hieght'] Commented Apr 4, 2020 at 12:35
  • I know this and I read the link before coming here. I would like to use apply. this is just a sample frame. Commented Apr 4, 2020 at 12:35
  • thank you @anky_91 your insights were highly valuable. Commented Apr 4, 2020 at 12:37

1 Answer 1

1

If you have to use the "apply" variant, the code should be:

df['product_AH'] = df.apply(lambda row: row.Age * row.Height, axis=1)

The parameter to the function applied is the whole row.

But much quicker solution is:

df['product_AH'] = df.Age * df.Height

(1.43 ms, compared to 5.08 ms for the "apply" variant).

This way computation is performed using vectorization, whereas apply refers to each row separately, applies the function to it, then assembles all results and saves them in the target column, which is considerably slower.

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.