Pandas: apply different functions to different columns
You can try a closure:
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'])
result = df.apply(multi_func({'A': np.mean, 'B': np.sum}))
I think you can use the agg
method with a dictionary as the argument. For example:
df = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]})
df =
A B
0 0 3
1 1 4
2 2 5
df.agg({'A': 'mean', 'B': sum})
A 1.0
B 12.0
dtype: float64
Just faced this situation myself and came up with the following:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([['one', 'two'], ['three', 'four'], ['five', 'six']],
...: columns=['A', 'B'])
In [3]: df
Out[3]:
A B
0 one two
1 three four
2 five six
In [4]: converters = {'A': lambda x: x[:1], 'B': lambda x: x.replace('o', '')}
In [5]: new = pd.DataFrame.from_dict({col: series.apply(converters[col])
...: if col in converters else series
...: for col, series in df.iteritems()})
In [6]: new
Out[6]:
A B
0 o tw
1 t fur
2 f six