Gmousse / dataframe-js

No Maintenance Intended
https://gmousse.gitbooks.io/dataframe-js/
MIT License
460 stars 38 forks source link

[QUESTION] Is there a way to add multiple aggregations to grouped dataframe? #107

Open olgacarpenter opened 4 years ago

olgacarpenter commented 4 years ago

Is there a way to add multiple aggregations to grouped dataframe? I would like to group the data and aggregate two different columns.

lobanov commented 4 years ago

There is no API for this, but I found you could achieve this with a bit extra work in exactly the same way aggregate(...) function works internally:


// assuming that sourceDf has columns ['key', 'col1', 'col2']
const groupedDf = sourceDf.groupBy('key');
const complexAggregateDf = new DataFrame([...groups].map(({groupKey, group}) => ({
  ...groupKey,
  'sum1': group.stat.sum('col1'),
  'sum2': group.stat.sum('col2'),
}), [...groupedDf.on, 'sum1', 'sum2']);```
adapptative commented 3 years ago

If somebody is wondering how to get this "groups" variable, I was able to get it by doing that:

const __groups__ = require('../node_modules/dataframe-js/lib/symbol').__groups__;
const groupedDf = sourceDf.groupBy('key');
const complexAggregateDf = new DataFrame(Object.values(groupedDf[__groups__]).map(({groupKey, group}) => ({
  ...groupKey,
  'sum1': group.stat.sum('col1'),
  'sum2': group.stat.sum('col2'),
}), [...groupedDf.on, 'sum1', 'sum2']);
romainhild commented 3 years ago

How would I do that from client-side ? I can't get the groups or __groups__ variable.