Gmousse/dataframe-js

Question: How to apply nested group-bys?

Closed this issue · 3 comments

How can I achieve the following structure? (Nested group-by).

Color State Name Qty Value
Red
VA
Jeff 2 $10
John 1 $5
PA
Rachel 4 $20
Blue
VA
Robert 5 $100
PA
Rebecca 3 $40

I have now something like the following:

    private recurseDataFrame(dataFrame: DataFrame | IGroupedDataFrame, group: IGroupByField, agg: IAggregateField): DataFrame {
        if (dataFrame instanceof DataFrame) {

            if (agg) {

                return dataFrame
                    .groupBy(...<string[]> group.name)
                    .aggregate((group) => dataFrame.stat[mathMap[agg.type]](agg.name));
            }

            return dataFrame.groupBy(...<string[]> group.name);

        } else {
            for (const dfGroup of dataFrame) {
                dfGroup.group = this.recurseDataFrame(dfGroup.group, group, agg);
            }

            return dataFrame;
        }
    }

One issue I have is that for (const dfGroup of dataFrame) is not normally supported by TypeScript when compiling to es5. By default, TypeScript only supports Array types for a for let or for const.

Enabling down-compilation of iterators causes other issues with the Angular framework, so it isn't a good solution for me,

Edit:

Is it as simple as:

            for (const dfGroup of dataFrame.toCollection()) {
                dfGroup.group = this.recurseDataFrame(dfGroup.group, group, agg);
            }

@Jefftopia Hi, have you resolved your computation issue ?

If you need to iterate on each groups of a groupedDataFrame you can use .toCollection (similar to [...yourGroupedDataFrame]).

It's also possible to groupBy inside an aggregate: df.groupBy(...).aggregate(gdf => gdf.groupBy(...).aggregate(...)) or to group on multiple keys (think tidy data instead of nested one).

Refers to: https://gmousse.gitbooks.io/dataframe-js/doc/api/groupedDataframe.html

Thanks, I resolved the issue.

Refers to: https://gmousse.gitbooks.io/dataframe-js/doc/api/groupedDataframe.html

@Gmousse, it seems the documentation content for GroupedDataFrame at the docs link is not being populated (I see an empty section when I load it). Is it possible that the page location has changed or something like that?

By the way, thank you for the info in this thread about working with GroupedDataFrames!