Pandas:df.groupby(x,y).apply()跨多个列参数错误

agc*_*nti 6 python pandas

基本问题:

我有几个'过去'和'现在'变量,我想执行一个简单的百分比变化' '.例如:((exports_now - exports_past)/exports_past)).

这两个问题实现了这一点,但是当我尝试类似的方法时,我得到一个错误,我的函数增量获得了一个未知参数axis.

数据示例:

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
Run Code Online (Sandbox Code Playgroud)

在第一个问题的答案之后,

我的解决方案是使用这样的函数:

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])
Run Code Online (Sandbox Code Playgroud)

并应用这样的功能:

df['exports_delta'] = df.groupby(['exports_past', 'exports_now']).apply(deltas, axis=1)
Run Code Online (Sandbox Code Playgroud)

这会产生此错误:TypeError: deltas() got an unexpected keyword argument 'axis' 有关如何绕过轴参数错误的任何想法?或者更优雅的方式来计算pct的变化?我的问题是我需要能够在几个不同的列对中应用此函数,因此难以对第二个问题中的答案进行硬编码.谢谢!

And*_*den 5

请考虑使用pct_changeSeries/DataFrame方法执行此操作.

df.pct_change()
Run Code Online (Sandbox Code Playgroud)

混淆源于两个不同(但同名)的apply函数,一个在Series/DataFrame上,另一个在groupby上.

In [11]: df
Out[11]:
   0  1  2
0  1  1  1
1  2  2  2
Run Code Online (Sandbox Code Playgroud)

数据帧应用方法需要一个轴的参数:

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
Run Code Online (Sandbox Code Playgroud)

GROUPBY所适用不和的kwarg传递给函数:

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'
Run Code Online (Sandbox Code Playgroud)

注意:groupby 确实有一个axis参数,所以你可以在那里使用它,如果你真的想:

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
Run Code Online (Sandbox Code Playgroud)