I'd like to aggregate a Pandas DataFrame along an axis using a custom function, and I'm having trouble figuring out what the function should return.
df = pd.DataFrame(np.arange(50).reshape(10,5))
You can pass numpy functions to DataFrame.agg:
# Case 1
df.agg([np.mean], axis=1)
And you get what you expect: a DataFrame indexed just like df, but with one column: 'mean'. But for some reason, the following behave completely differently:
# Case 2
df.agg([lambda x:np.mean(x)], axis=1)
or even
# Case 3
def f(x, **kwargs):
return np.mean(x, **kwargs)
df.agg([f], axis=1)
Why should the latter two work any differently than the first case?