user9933484
user9933484

Reputation: 29

Applying many functions to the same column

I have a column in the dataframe on which I apply many functions. For example,

df[col_name] = df[col_name].apply(lambda x: fun1(x))
df[col_name] = df[col_name].apply(lambda x: fun2(x))
df[col_name] = df[col_name].apply(lambda x: fun3(x))

I have 10 functions that I apply to this column for preprocessing and cleaning. Is there a way where I can refactor this code or make the block of code small?

Upvotes: 0

Views: 49

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10959

How about

def fun(x):
    for f in (fun1, fun2, fun3):
        x = f(x)

    return x


df[col_name] = df[col_name].apply(fun)

Upvotes: 2

Related Questions