Skip to content

Commit 5444602

Browse files
committed
reformat the multindex dataframe fro groupby
1 parent 3d45999 commit 5444602

2 files changed

Lines changed: 109 additions & 19 deletions

File tree

danfojs/src/core/groupby.js

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,8 @@ export class GroupBy {
208208
}else{
209209
data = eval(`this.group_col[key1][key2][i].${operation}`)
210210
}
211-
if(Array.isArray(data)){
212-
count_group[key1][key2].push(...data)
213-
}else{
214-
count_group[key1][key2].push(data)
215-
}
211+
count_group[key1][key2].push(data)
212+
216213
}
217214

218215
}
@@ -237,11 +234,8 @@ export class GroupBy {
237234
}else{
238235
data = eval(`this.group_col[key1][i].${operation}`)
239236
}
240-
if(Array.isArray(data)){
241-
count_group[key1].push(...data)
242-
}else{
243-
count_group[key1].push(data)
244-
}
237+
238+
count_group[key1].push(data)
245239

246240
}
247241
}
@@ -367,20 +361,43 @@ export class GroupBy {
367361

368362
to_DataFrame(key_col,col,data,ops){
369363

364+
// console.log(data);
370365
if(key_col.length ==2){
371-
372366
let df_data = []
373367
for(let key_1 in data){
374368

375369
let key_val = data[key_1]
376370

377371
for(let key_2 in key_val){
378372
let k_data = key_val[key_2]
379-
let kk = []
380-
kk[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
381-
kk[1] = isNaN(parseInt(key_2)) ? key_2 : parseInt(key_2)
382-
kk.push(...k_data)
383-
df_data.push(kk)
373+
let key_data = []
374+
375+
376+
if(Array.isArray(k_data[0])){
377+
for(let i=0; i < k_data.length; i++){
378+
let col_data = k_data[i]
379+
380+
for (let j=0; j < col_data.length; j++ ){
381+
382+
if(typeof key_data[j] === "undefined" ){
383+
key_data[j] = []
384+
key_data[j][0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
385+
key_data[j][1] = isNaN(parseInt(key_2)) ? key_2 : parseInt(key_2)
386+
key_data[j].push(col_data[j])
387+
}else{
388+
key_data[j].push(col_data[j]);
389+
}
390+
}
391+
}
392+
df_data.push(...key_data)
393+
394+
}else{
395+
key_data[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
396+
key_data[1] = isNaN(parseInt(key_2)) ? key_2 : parseInt(key_2)
397+
key_data.push(...k_data)
398+
df_data.push(key_data)
399+
}
400+
384401

385402
}
386403

@@ -402,9 +419,29 @@ export class GroupBy {
402419
let key_val = data[key_1]
403420

404421
let key_data = []
405-
key_data[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
406-
key_data.push(...key_val)
407-
df_data.push(key_data)
422+
if(Array.isArray(key_val[0])){
423+
for(let i=0; i < key_val.length; i++){
424+
let col_data = key_val[i]
425+
426+
for (let j=0; j < col_data.length; j++ ){
427+
428+
if(typeof key_data[j] === "undefined" ){
429+
key_data[j] = []
430+
key_data[j][0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
431+
key_data[j].push(col_data[j])
432+
}else{
433+
key_data[j].push(col_data[j]);
434+
}
435+
}
436+
df_data.push(...key_data)
437+
}
438+
439+
}else{
440+
key_data[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
441+
key_data.push(...key_val)
442+
df_data.push(key_data)
443+
}
444+
408445
}
409446
let column = [...key_col]
410447
let group_col = col.slice().map((x,i)=>{

danfojs/tests/core/groupby.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,57 @@ describe("groupby", function () {
181181
assert.deepEqual(group_df.col(["B", "C"]).mean().values, new_data);
182182
});
183183

184+
it("printing multiindex table, example with cumsum operation for dataframe group by one column",function(){
185+
let data =[{'A': ['foo', 'bar', 'foo', 'bar',
186+
'foo', 'bar', 'foo', 'foo']},
187+
{'B': ['one', 'one', 'two', 'three',
188+
'two', 'two', 'one', 'three']},
189+
{'C': [1,3,2,4,5,2,6,7]},
190+
{'D': [3,2,4,1,5,6,7,8]}
191+
]
192+
193+
let df = new DataFrame(data)
194+
195+
196+
let grp = df.groupby(["A"])
197+
let rslt = [
198+
[ 'foo', 1 ],
199+
[ 'foo', 3 ],
200+
[ 'foo', 8 ],
201+
[ 'foo', 14 ],
202+
[ 'foo', 21 ],
203+
[ 'bar', 3 ],
204+
[ 'bar', 7 ],
205+
[ 'bar', 9 ]
206+
]
207+
assert.deepEqual(grp.col(["C"]).cumsum().values, rslt);
208+
209+
})
210+
it("printing multiindex table, example with cumsum operation for dataframe group by one column",function(){
211+
let data =[{'A': ['foo', 'bar', 'foo', 'bar',
212+
'foo', 'bar', 'foo', 'foo']},
213+
{'B': ['one', 'one', 'two', 'three',
214+
'two', 'two', 'one', 'three']},
215+
{'C': [1,3,2,4,5,2,6,7]},
216+
{'D': [3,2,4,1,5,6,7,8]}
217+
]
218+
219+
let df = new DataFrame(data)
220+
221+
222+
let grp = df.groupby(["A","B"])
223+
let rslt = [
224+
[ 'foo', 'one', 1, 3 ],
225+
[ 'foo', 'one', 7, 10 ],
226+
[ 'foo', 'two', 2, 4 ],
227+
[ 'foo', 'two', 7, 9 ],
228+
[ 'foo', 'three', 7, 8 ],
229+
[ 'bar', 'one', 3, 2 ],
230+
[ 'bar', 'two', 2, 6 ],
231+
[ 'bar', 'three', 4, 1 ]
232+
]
233+
assert.deepEqual(grp.col(["C","D"]).cumsum().values,rslt)
234+
235+
})
236+
184237
});

0 commit comments

Comments
 (0)