Closed
Description
When using DataFrame.apply(), it would be helpful to be able to apply different functions to different columns. There's a nice workaround on StackOverflow:
import numpy as np
import pandas as pd
def multi_func(functions):
def f(col):
return functions[col.name](col)
return f
df = pd.DataFrame(np.random.random((10, 2)), columns=['A', 'B'])
results = df.apply(multi_func({'A': np.mean, 'B': np.sum}))
results
A 0.401456
B 6.845529
dtype: float64
My guess is that the changes would be made in pandas.core.frame.DataFrame, potentially adding a new _apply_X
method. Any thoughts/advice?