DataFrame.groupby
Group DataFrame using a mapper or by a Series of columns.
danfo.DataFrame.groupby(columns) [source]
Parameters
Type
Description
Default
columns
Array
The names of a column(s) in the DataFrame to group by
Returns:
return DataFrame.groups

Examples

Groupby a single column from a DataFrame

Node
Browser
1
const dfd = require("danfojs-node")
2
3
let data = [["Pear", 2, 3], ["Pear", 5, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
4
let cols = ["A", "B", "C"]
5
6
let df = new dfd.DataFrame(data, { columns: cols })
7
let group_df = df.groupby(["A"])
8
console.log(group_df)
9
10
//GroupBy Object
11
GroupBy {
12
key_col: [ 'A' ],
13
col_dict: { Pear: [ [Array], [Array] ], Apple: [ [Array], [Array] ] },
14
data: [
15
[ 'Pear', 2, 3 ],
16
[ 'Pear', 5, 6 ],
17
[ 'Apple', 30, 40 ],
18
[ 'Apple', 89, 78 ]
19
],
20
column_name: [ 'A', 'B', 'C' ],
21
data_tensors: {
22
Pear: DataFrame {
23
'$isSeries': false,
24
'$config': [Configs],
25
'$data': [Array],
26
'$dataIncolumnFormat': [Array],
27
'$index': [Array],
28
'$dtypes': [Array],
29
'$columns': [Array]
30
},
31
Apple: DataFrame {
32
'$isSeries': false,
33
'$config': [Configs],
34
'$data': [Array],
35
'$dataIncolumnFormat': [Array],
36
...
37
'$columns': [Array]
38
}
39
},
40
col_dtype: [ 'string' ]
41
}
Copied!
1
Copied!
A groupby operation will return a GroupBy class object. You can apply any of the following operation on the groupby result:
    1.
    count
    2.
    sum
    3.
    std
    4.
    var
    5.
    mean
    6.
    cumsum
    7.
    cummax
    8.
    cumprod
    9.
    cummin
    10.
    max
    11.
    min

Example of Groupby and apply a sum function

Node
1
const dfd = require("danfojs-node")
2
3
let data = [["Pear", 2, 3], ["Pear", 5, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
4
let cols = ["A", "B", "C"]
5
let df = new dfd.DataFrame(data, { columns: cols })
6
let group_df = df.groupby(["A"]).sum()
7
8
group_df.print()
9
Copied!
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ A │ B_sum │ C_sum ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 0 │ Pear │ 7 │ 9 ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 1 │ Apple │ 119 │ 118 ║
7
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!

Groupby a two columns from a DataFrame

Node
Browser
1
const dfd = require("danfojs-node")
2
3
let data = [["Pear", 2, 3], ["Pear", 2, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
4
let cols = ["A", "B", "C"]
5
let df = new dfd.DataFrame(data, { columns: cols })
6
let group_df = df.groupby(["A", "B"]).sum()
7
8
group_df.print()
Copied!
1
Copied!
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ A │ B │ C_sum ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 0 │ Pear │ 2 │ 9 ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 1 │ Apple │ 30 │ 40 ║
7
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
8
║ 2 │ Apple │ 89 │ 78 ║
9
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!
Last modified 6d ago