Gmousse / dataframe-js

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

Question: How to apply nested group-bys? #28

Closed Jefftopia closed 6 years ago

Jefftopia commented 6 years ago

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);
            }
Gmousse commented 6 years ago

@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

Jefftopia commented 6 years ago

Thanks, I resolved the issue.

mike-incipia commented 4 years ago

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!