Reputation: 657
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?
Upvotes: 1
Views: 1335
Reputation: 1
I created a small package to do this for Pandas/Polars .pipe
operations, or any other arbitrary functions: https://chris-santiago.github.io/dpipes/
Upvotes: 0
Reputation: 1525
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)
Upvotes: 6