Skip to content

Commit 56803dc

Browse files
committed
add get_groups and agg method to groupby
1 parent 5eeb23e commit 56803dc

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/danfojs-base/aggregators/groupby.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ArrayType1D, ArrayType2D } from "../shared/types"
44
import { variance, std, median, mode, mean } from 'mathjs';
55
import Utils from "../shared/utils";
66
import concat from "../transformers/concat"
7+
import Series from "../core/series";
78

89

910
/**
@@ -16,7 +17,7 @@ import concat from "../transformers/concat"
1617
* @param {colDtype} Array columns dtype
1718
*/
1819
export default class Groupby {
19-
colDict?: { [key: string ]: {} }
20+
colDict: { [key: string ]: {} } = {}
2021
keyCol: ArrayType1D
2122
data?: ArrayType2D | null
2223
columnName: ArrayType1D
@@ -379,4 +380,28 @@ export default class Groupby {
379380
min(){
380381
return this.operations("min")
381382
}
383+
384+
get_groups(keys: Array<string>): DataFrame {
385+
let dictKey = keys.join("_")
386+
let colDict: { [key: string ]: {} } = {}
387+
colDict[dictKey] = {...this.colDict[dictKey]}
388+
return this.toDataFrame(colDict)
389+
}
390+
391+
agg(ops: { [key: string ]: Array<string> | string }) {
392+
let columns = Object.keys(ops);
393+
let col_gp = this.col(columns);
394+
let data = col_gp.arithemetic(ops);
395+
let df = col_gp.toDataFrame(data);
396+
return df;
397+
}
398+
399+
apply(callable: (x: DataFrame)=> DataFrame | Series ) {
400+
let colDict: { [key: string ]: DataFrame | Series } = {}
401+
for(const [key, values] of Object.entries(this.colDict)) {
402+
let valDataframe = new DataFrame(values)
403+
colDict[key] = callable(valDataframe)
404+
}
405+
406+
}
382407
}

0 commit comments

Comments
 (0)