danfo.Groupby.cumsum() [source]
Parameters: None
Return: DataFrame
Examples
Obtain the cumulative sum of a column for each groups, group by one column
const dfd = require("danfojs-node")let data ={'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],'B': ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],'C': [1,3,2,4,5,2,6,7],'D': [3,2,4,1,5,6,7,8]}let df = new dfd.DataFrame(data)let grp = df.groupby(["A"])grp.col(["C"]).cumsum().head().print()grp.col(["C"]).cumsum().tail().print()
Shape: (5,2)╔═══╤═══════════════════╤═══════════════════╗║ │ A │ C_cumsum ║╟───┼───────────────────┼───────────────────╢║ 0 │ foo │ 1 ║╟───┼───────────────────┼───────────────────╢║ 1 │ foo │ 3 ║╟───┼───────────────────┼───────────────────╢║ 2 │ foo │ 8 ║╟───┼───────────────────┼───────────────────╢║ 3 │ foo │ 14 ║╟───┼───────────────────┼───────────────────╢║ 4 │ foo │ 21 ║╚═══╧═══════════════════╧═══════════════════╝Shape: (5,2)╔═══╤═══════════════════╤═══════════════════╗║ │ A │ C_cumsum ║╟───┼───────────────────┼───────────────────╢║ 3 │ foo │ 14 ║╟───┼───────────────────┼───────────────────╢║ 4 │ foo │ 21 ║╟───┼───────────────────┼───────────────────╢║ 5 │ bar │ 3 ║╟───┼───────────────────┼───────────────────╢║ 6 │ bar │ 7 ║╟───┼───────────────────┼───────────────────╢║ 7 │ bar │ 9 ║╚═══╧═══════════════════╧═══════════════════╝
Obtain the cumsum for two columns for each group, group by one column
const dfd = require("danfojs-node")let data ={'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],'B': ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],'C': [1,3,2,4,5,2,6,7],'D': [3,2,4,1,5,6,7,8]}let df = new dfd.DataFrame(data)let grp = df.groupby(["A"])grp.col(["C","D"]).cumsum().head().print()grp.col(["C","D"]).cumsum().tail().print()
Shape: (5,3)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ C_cumsum │ D_cumsum ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 0 │ foo │ 1 │ 3 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 1 │ foo │ 3 │ 7 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 2 │ foo │ 8 │ 12 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 3 │ foo │ 14 │ 19 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 4 │ foo │ 21 │ 27 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝Shape: (5,3)╔════╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ C_cumsum │ D_cumsum ║╟────┼───────────────────┼───────────────────┼───────────────────╢║ 11 │ bar │ 7 │ 3 ║╟────┼───────────────────┼───────────────────┼───────────────────╢║ 12 │ bar │ 9 │ 9 ║╟────┼───────────────────┼───────────────────┼───────────────────╢║ 13 │ bar │ 3 │ 2 ║╟────┼───────────────────┼───────────────────┼───────────────────╢║ 14 │ bar │ 7 │ 3 ║╟────┼───────────────────┼───────────────────┼───────────────────╢║ 15 │ bar │ 9 │ 9 ║╚════╧═══════════════════╧═══════════════════╧═══════════════════╝
Obtain the cumsum for a column for each group, group by two columns
const dfd = require("danfojs-node")let data ={'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],'B': ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],'C': [1,3,2,4,5,2,6,7],'D': [3,2,4,1,5,6,7,8]}let df = new dfd.DataFrame(data)let grp = df.groupby(["A","B"])grp.col(["C"]).cumsum().head().print()grp.col(["C"]).cumsum().tail().print()
Shape: (5,3)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C_cumsum ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 0 │ foo │ one │ 1 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 1 │ foo │ one │ 7 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 2 │ foo │ two │ 2 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 3 │ foo │ two │ 7 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 4 │ foo │ three │ 7 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝Shape: (5,3)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C_cumsum ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 3 │ foo │ two │ 7 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 4 │ foo │ three │ 7 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 5 │ bar │ one │ 3 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 6 │ bar │ two │ 2 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 7 │ bar │ three │ 4 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Obtain the count for two columns for each group, group by two columns
const dfd = require("danfojs-node")let data ={'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],'B': ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],'C': [1,3,2,4,5,2,6,7],'D': [3,2,4,1,5,6,7,8]}let df = new dfd.DataFrame(data)let grp = df.groupby(["A","B"])grp.col(["C","D"]).cumsum().head().print()grp.col(["C","D"]).cumsum().tail().print()
Shape: (5,4)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C_cumsum │ D_cumsum ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 0 │ foo │ one │ 1 │ 3 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 1 │ foo │ one │ 7 │ 10 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 2 │ foo │ two │ 2 │ 4 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 3 │ foo │ two │ 7 │ 9 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 4 │ foo │ three │ 7 │ 8 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝Shape: (5,4)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C_cumsum │ D_cumsum ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 3 │ foo │ two │ 7 │ 9 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 4 │ foo │ three │ 7 │ 8 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 5 │ bar │ one │ 3 │ 2 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 6 │ bar │ two │ 2 │ 6 ║╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢║ 7 │ bar │ three │ 4 │ 1 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝