Home > OS >  How to use functions with several paramiters in a groupby
How to use functions with several paramiters in a groupby

Time:09-23

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 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?

CodePudding user response:

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,
    ],
)
  • Related