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.136555
Or 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
df1 = df.groupby('lvl_1')['value'].agg(['min','max',("custom_quantile",lambda x: x.quantile(q))])
for q=0.22, the output is:
min max custom_quantile
lvl_1
di 0.275401 0.530000 0.294589
fi 0.054363 0.848818 0.136555
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With