python - Pandas: df.groupby(x, y).apply() across multiple columns parameter error -
basic problem:
i have several 'past' , 'present' variables i'd perform simple percent change 'row-wise' on. example: ((exports_now - exports_past)/exports_past))
.
these 2 questions accomplish when try similar method error function deltas gets unknown parameter axis
.
data example :
exports_ past exports_ imports_ past imports_ 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
following answer in first question,
my solution use function this:
def deltas(row): ''' simple pct change ''' if int(row[0]) == 0 , int(row[1]) == 0: return 0 elif int(row[0]) == 0: return np.nan else: return ((row[1] - row[0])/row[0])
and apply function this:
df['exports_delta'] = df.groupby(['exports_past', 'exports_now']).apply(deltas, axis=1)
this generates error : typeerror: deltas() got unexpected keyword argument 'axis'
ideas on how around axis parameter error? or more elegant way calculate pct change? kicker problem needs able apply function across several different column pairs, hard coding column names answer in 2nd question undesirable. thanks!
consider using pct_change
series/dataframe method this.
df.pct_change()
the confusion stems 2 different (but equally named) apply
functions, 1 on series/dataframe , 1 on groupby.
in [11]: df out[11]: 0 1 2 0 1 1 1 1 2 2 2
the dataframe apply method takes 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
the groupby apply doesn't, , kwarg passed 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 unexpected keyword argument 'axis'
note: groupby does have axis argument, can use there, if want to:
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
Comments
Post a Comment