Reputation: 11
I have a dataframe called df and I have to apply a function called fn to it. I tried the below 2 methods. Both of them are fetching me the same results, but I want to know the difference between them.
df = df.apply(fn)
df = df.apply(lambda x : fn(x))
Please tell me what's the difference in these two lines.
Upvotes: 1
Views: 94
Reputation: 818
Actually there is no big difference.
In the first example, you are passing the function directly to the apply()
method and the method will then use the passed instance to call the fn function for each data.
In the second example you are creating something called an anonymous function (lambda function) which is just a one-line function for simple purposes. The second example will be equivalent to the code-snippet below if you weren't using lambda functions.
def lmbda(x):
return fn(x)
df.apply(lmbda)
Upvotes: 1