Issue
I have written the following code to pipe some transformations of a dataset:
def func(self,df):
...
return (df.pipe(self.transformations[0])
.pipe(self.transformations[1]
.
.pipe(self.transformations[n])
I'd like to rewrite it in a more pythonic way, abstracting away from the number of elements in the list transformations, so to not have to hard code the transformations, but just add them to the list.
I thought about a for loop in which I update the dataframe at each iteration with a transformation, but I'd like a more concise way of doing this.
Any suggestion?
Solution
Consider using reduce
which can replace loops and recursion much of the time.
from functools import reduce
def func(self, df):
return reduce(lambda _df, trans: _df.pipe(trans), self.transformations, df)
Answered By - Kyle Parsons
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.