本文介绍了将groupby.agg中的参数传递给多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都知道如何用多个函数在groupby.agg()中传递参数?
底线,我想用一个自定义函数,但我会用一个需要参数的内置函数来问我的问题。
假设:
使用 lambda 函数:q = 0.22
df1 = df.groupby('lvl_1')['value']。agg(['' min','max',lambda x:x.quantile(q)])
print(df1)
min max< lambda>
lvl_1
di 0.275401 0.530000 0.294589
fi 0.054363 0.848818 0.136555
或者可以创建 f 函数并为自定义列名设置名称:
q = 0.22
f = lambda x:x.quantile(q)
f .__ name__ ='custom_quantile'
df1 = df.groupby('lvl_1')['value']。 agg(['min','max',f])
print(df1)
min min custom_quantile
lvl_1
di 0.275401 0.530000 0.294589
fi 0.054363 0.848818 0.136555
Anyone knows how to pass arguments in a groupby.agg() with multiple functions?
Bottom line, I would like to use it with a custom function, but I will ask my question using a built-in function needing an argument.
Assuming:
import pandas as pd import numpy as np import datetime np.random.seed(15) day = datetime.date.today() day_1 = datetime.date.today() - datetime.timedelta(1) day_2 = datetime.date.today() - datetime.timedelta(2) day_3 = datetime.date.today() - datetime.timedelta(3) ticker_date = [('fi', day), ('fi', day_1), ('fi', day_2), ('fi', day_3), ('di', day), ('di', day_1), ('di', day_2), ('di', day_3)] index_df = pd.MultiIndex.from_tuples(ticker_date, names=['lvl_1', 'lvl_2']) df = pd.DataFrame(np.random.rand(8), index_df, ['value'])How would I do this:
df.groupby('lvl_1').agg(['min','max','quantile'])with, as argument for 'quantile':
q = 0.22解决方案Use lambda function:
q = 0.22 df1 = df.groupby('lvl_1')['value'].agg(['min','max',lambda x: x.quantile(q)]) print (df1) min max <lambda> lvl_1 di 0.275401 0.530000 0.294589 fi 0.054363 0.848818 0.136555Or is possible create f function and set it name for custom column name:
q = 0.22 f = lambda x: x.quantile(q) f.__name__ = 'custom_quantile' df1 = df.groupby('lvl_1')['value'].agg(['min','max',f]) print (df1) min max custom_quantile lvl_1 di 0.275401 0.530000 0.294589 fi 0.054363 0.848818 0.136555
这篇关于将groupby.agg中的参数传递给多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!