Skip to content
Advertisement

Applying custom function to groupby object keeps groupby column

I have a dataframe which as a column for grouping by and several other columns. Play dataframe:

JavaScript

When using a groupby on this dataframe followed by a default function, the groupby column is set as an index and not included in the results:

JavaScript

enter image description here

But when I define a custom function and use apply, I get an unwanted additional column:

JavaScript

enter image description here

How do I avoid having this additional column?

The actual function I want to use is the following:

JavaScript

Advertisement

Answer

You can try to use .agg instead of .apply:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement