So I have quite big DataFrame and I create a new column by some equation based on other columns:
df['F'] = (params.a * params.b * df.A/1000 - param.C * (df.B + df.C - df.D) + param.D * df.E
and it works perfectly fine. Except I want to repeat this function throughout the code , so instead of error-prone copying and pasting I want to cast it into a reusable function.
So I casted it into lambda:
def fun(r):
return (params.a * params.b * r.A/1000 - param.C * (r.B + r.C - r.D) + param.D * r.E
df['F'] = r.apply(funy,axis =1)
yet this is 5x slower now ( 1.2s vs 6s for 10k rows).
What should I do if I want to have a neat function and still benefit from speed?
What's wrong with:
def fun():
return params.a * params.b * df.A/1000 - param.C * (df.B + df.C - df.D) + param.D * df.E
df['F'] = fun()
So you get a reusable vectorized function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.