ninazzo
ninazzo

Reputation: 657

Pandas pipe a list of functions

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

Answers (2)

Chris Santiago
Chris Santiago

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

Kyle Parsons
Kyle Parsons

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

Related Questions