How to use functions with several paramiters in a groupby

36 Views Asked by At

I have the following dataset for which I want to calculate several aggregation metrics>

enter image description here

For some I'm using the standard functions, but for other I relay on the tsfresh library, from where I'm importing the functions:

sample.groupby('id').agg(['std', benford_correlation,absolute_maximum])

It works well for functions that have only one parameter, as is the case of:

from tsfresh.feature_extraction.feature_calculators import benford_correlation #(x)
from tsfresh.feature_extraction.feature_calculators import absolute_maximum #(x)

But for others like:

from tsfresh.feature_extraction.feature_calculators import autocorrelation#(x, lag)

enter image description here

I get and error since it has two parameters, x and lag by I'm only passing the x implicitly in the groupby.

How can I specify the other parameters required?

1

There are 1 best solutions below

0
Michael Delgado On BEST ANSWER

see the pandas.DataFrameGroupBy.aggregate docs. Additional keyword arguments are passed to the function. So you can do this:

sample.groupby('id').agg(
    ['std', benford_correlation,absolute_maximum],
    additional_arg=value,
)

but if you need to pass different arguments to each function, you could use a lambda function:

sample.groupby('id').agg(
    [
        'std',
        lambda s: benford_correlation(s, lag=1),
        absolute_maximum,
    ],
)