Using the .apply method
In this section, we will be using the .apply
method to create custom data aggregation functions that can be applied to our grouped data.
The .apply
method enables custom functions to be applied to grouped data. It is the major function of the split-apply-combine method discussed earlier in this chapter.
The groupby
method implemented in Danfo.js only contains a small set of data aggregation methods needed for group data, hence the .apply
method gives users the ability to construct a special data aggregation method from the grouped data.
Using the previous data, we will create a new DataFrame excluding column B
as seen in the previous DataFrame, and then create a custom function that will be applied to the grouped data:
let group_df = df.groupby([ "A"]); const add = (x) => { return x.add(2); }; group_df.apply(add).print();
In the preceding code, we group the DataFrame by column A
, and then proceed to create a custom function...