To do this, use the pct_change Series / DataFrame method.
df.pct_change()
The confusion stems from two different (but equally named) apply functions, one of which is for the / DataFrame series and one for groupby.
In [11]: df Out[11]: 0 1 2 0 1 1 1 1 2 2 2
The DataFrame apply method takes an axis argument:
In [12]: df.apply(lambda x: x[0] + x[1], axis=0) Out[12]: 0 3 1 3 2 3 dtype: int64 In [13]: df.apply(lambda x: x[0] + x[1], axis=1) Out[13]: 0 2 1 4 dtype: int64
groupby apply is not executed, and kwarg is passed to the function:
In [14]: g.apply(lambda x: x[0] + x[1]) Out[14]: 0 2 1 4 dtype: int64 In [15]: g.apply(lambda x: x[0] + x[1], axis=1) TypeError: <lambda>() got an unexpected keyword argument 'axis'
Note: this group has an axis argument, so you can use it there if you really want:
In [16]: g1 = df.groupby(0, axis=1) In [17]: g1.apply(lambda x: x.iloc[0, 0] + x.iloc[1, 0]) Out[17]: 0 1 3 2 3 dtype: int64
source share