Pandas: df.groupby (x, y) .apply () for multiple columns parameter error

Main problem:

I have several "past" and "real" variables that I would like to accomplish with a simple percent change in row-wise. For example: ((exports_now - exports_past)/exports_past)) .

These two questions achieve this, but when I try to use a similar method, I get an error that my deltas function receives an unknown axis parameter.

Sample data:

 exports_ past exports_ now imports_ past imports_ now ect.(6 other pairs) .23 .45 .43 .22 1.23 .13 .21 .47 .32 .23 0 0 .41 .42 .93 .23 .66 .43 .22 .21 0 .12 .47 .21 1.23 

After the answer in the first question

My solution is to use a function like this:

 def deltas(row): ''' simple pct change ''' if int(row[0]) == 0 and int(row[1]) == 0: return 0 elif int(row[0]) == 0: return np.nan else: return ((row[1] - row[0])/row[0]) 

And apply this function as follows:

 df['exports_delta'] = df.groupby(['exports_past', 'exports_now']).apply(deltas, axis=1) 

This TypeError: deltas() got an unexpected keyword argument 'axis' this error: TypeError: deltas() got an unexpected keyword argument 'axis' Any ideas on how to get around the axis parameter error? Or a more elegant way to calculate pct changes? The kicker with my problem is that I need to be able to apply this function to several different pairs of columns, so hard-coding column names, such as the answer in the second question, is undesirable. Thanks!

+6
source share
1 answer

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 
+5
source

Source: https://habr.com/ru/post/950730/


All Articles