Vikas J
Vikas J

Reputation: 358

Apply series of transformations to pandas DataFrame object

I am relatively new to Python programming. I have a pandas DataFrame object say obj1. I need to apply a series of transformation to records stored in obj1['key']. Suppose obj1['key'] has 300 entries and I need to apply func1 then func2 then func3 on each of the 300 entries and store the final result in obj1['key']. One way would be to do as below. Is there a better way to do the same?

obj1['key']=[func3(func2(func1(item))) for item in obj1['key']]

Python generators can't be used for this purpose right.

Upvotes: 0

Views: 135

Answers (2)

piRSquared
piRSquared

Reputation: 294338

define a function

def recursivator(x, fs):
    return recursivator(fs[-1](x), fs[:-1]) if len(fs) > 0 else x

x is the thing being operated on, fs is a list of functions.

df = df.applymap(lambda x: recursivator(x, fs))

Upvotes: 0

hsrv
hsrv

Reputation: 1570

Yes, you could use the default method DataFrame.apply()

df = df.apply(f1).apply(f2).apply(fn)

Upvotes: 1

Related Questions