[英]Custom function in Pandas aggregation
I have a following dataframe - result from this question :我有以下 dataframe - 这个问题的结果:
t2m ... kont sum d1 d2
latitude longitude ...
46.5 18.0 0.284698 ... 0.001613 1998-01-12 07:00:00 1998-01-24 08:00:00
18.0 -1.304504 ... FROMHERE 0.004097 1998-01-24 08:00:00 1998-01-24 09:00:00
18.0 0.345001 ... FROMHERE 0.024207 1998-01-24 17:00:00 1998-01-25 00:00:00
18.0 -4.786346 ... FROMHERE xxxxxx
I want to implement combination of custom and build in functions to .agg
of this dataframe.我想实现自定义和内置函数的组合到这个.agg
的 .agg。 Here is the code:这是代码:
dfgeo=df.groupby(['latitude', 'longitude']).agg(
std=('sum',np.std),
maks=('sum','max'),
mean=('sum',(lambda x: mean(absolute(x - mean(x)))))
).reset_index()
Code mean=('sum',(lambda x: mean(absolute(x - mean(x)))))
mimics Mean Average Deviation since is not directly build in Numpy, or i cant find it.代码mean=('sum',(lambda x: mean(absolute(x - mean(x)))))
模仿平均平均偏差,因为不是直接构建在 Numpy 中,或者我找不到它。 I get following error:我收到以下错误:
KeyError: "[('ar', '<lambda>')] not in index"
Any help is appreciated.任何帮助表示赞赏。
For me working custom function:对我来说,定制 function:
def f(x):
return np.mean(np.abs(x - np.mean(x)))
dfgeo=df.groupby(['latitude', 'longitude']).agg(
std=('sum',np.std),
maks=('sum','max'),
mean=('sum',f)
).reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.