Learning is a mess
Learning is a mess

Reputation: 8277

Pandas: apply list of functions on columns, one function per column

Setting: for a dataframe with 10 columns I have a list of 10 functions which I wish to apply in a function1(column1), function2(column2), ..., function10(column10) fashion. I have looked into pandas.DataFrame.apply and pandas.DataFrame.transform but they seem to broadcast and apply each function on all possible columns.

Upvotes: 2

Views: 980

Answers (5)

Levi Baguley
Levi Baguley

Reputation: 738

Similar to @Chris Adams's answer but makes a copy of the dataframe using dictionary comprehension and zip.

def function1(x):
    return x + 1

def function2(x):
    return x * 2

def function3(x):
    return x**2

df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 2, 3], 'C': [1, 2, 3]})
functions = [function1, function2, function3]
print(df)

#    A  B  C
# 0  1  1  1
# 1  2  2  2
# 2  3  3  3

df_2 = pd.DataFrame({col: func(df[col]) for col, func in zip(df, functions)})
print(df_2)

#    A  B  C
# 0  2  2  1
# 1  3  4  4
# 2  4  6  9

Upvotes: 0

Yefet
Yefet

Reputation: 2086

You can simply apply to specific column

df['x'] = df['X'].apply(lambda x: x*2)

Upvotes: 0

Oscar Mike
Oscar Mike

Reputation: 744

You can probably try to map your N functions to each row by using a lambda containing a Series with your operations, check the following code:

import pandas as pd

matrix = [(22, 34, 23), (33, 31, 11), (44, 16, 21), (55, 32, 22), (66, 33, 27),
(77, 35, 11)]

df = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef'))

Will produce:

    x   y   z
a  22  34  23
b  33  31  11
c  44  16  21
d  55  32  22
e  66  33  27
f  77  35  11

and then:

res_df = df.apply(lambda row: pd.Series([row[0] + 1, row[1] + 2, row[2] + 3]), axis=1)

will give you:

    0   1   2
a  23  36  26
b  34  33  14
c  45  18  24
d  56  34  25
e  67  35  30
f  78  37  14

Upvotes: 1

Chris Adams
Chris Adams

Reputation: 18647

IIUC, with zip and a for loop:

Example

def function1(x):
    return x + 1

def function2(x):
    return x * 2

def function3(x):
    return x**2

df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 2, 3], 'C': [1, 2, 3]})
functions = [function1, function2, function3]

print(df)

#    A  B  C
# 0  1  1  1
# 1  2  2  2
# 2  3  3  3


for col, func in zip(df, functions):
    df[col] = df[col].apply(func)

print(df)

#    A  B  C
# 0  2  2  1
# 1  3  4  4
# 2  4  6  9

Upvotes: 2

YOLO
YOLO

Reputation: 21749

You could do something like:

# list containing functions
fun_list = []

# assume df is your dataframe
for i, fun in enumerate(fun_list):
    df.iloc[:,i] = fun(df.iloc[:,i])

Upvotes: 1

Related Questions