|
| 1 | +--- |
| 2 | +description: Obtain the cumulative product per group for each column |
| 3 | +--- |
| 4 | + |
| 5 | +# Groupby.cumProd |
| 6 | + |
| 7 | +> danfo.Groupby.**cumprod**\(\) \[[source](https://github.com/javascriptdata/danfojs/blob/65f9b3753389b08101d4bb00a2d6488255476aaf/src/danfojs-base/aggregators/groupby.ts#L489)\] |
| 8 | +
|
| 9 | +**Parameters**: None |
| 10 | + |
| 11 | +**Return**: DataFrame |
| 12 | + |
| 13 | +**Examples** |
| 14 | + |
| 15 | +Obtain the cumulative product of a column for each groups, group by one column |
| 16 | + |
| 17 | +{% tabs %} |
| 18 | +{% tab title="Node" %} |
| 19 | +```javascript |
| 20 | +const dfd = require("danfojs-node") |
| 21 | + |
| 22 | + |
| 23 | +let data ={A: ['foo', 'bar', 'foo', 'bar', |
| 24 | + 'foo', 'bar', 'foo', 'foo'], |
| 25 | + B: ['one', 'one', 'two', 'three', |
| 26 | + 'two', 'two', 'one', 'three'], |
| 27 | + C: [1,3,2,4,5,2,6,7], |
| 28 | + D: [3,2,4,1,5,6,7,8] |
| 29 | +} |
| 30 | + |
| 31 | +let df = new dfd.DataFrame(data) |
| 32 | + |
| 33 | +let grp = df.groupby(["A"]) |
| 34 | +let grpCol = grp.col(["C"]) |
| 35 | +grpCol.cumProd().head().print() |
| 36 | +grpCol.cumProd().tail().print() |
| 37 | +``` |
| 38 | +{% endtab %} |
| 39 | +{% endtabs %} |
| 40 | + |
| 41 | +```text |
| 42 | +
|
| 43 | + Shape: (5,2) |
| 44 | +
|
| 45 | +╔═══╤═══════════════════╤═══════════════════╗ |
| 46 | +║ │ A │ C_cumprod ║ |
| 47 | +╟───┼───────────────────┼───────────────────╢ |
| 48 | +║ 0 │ foo │ 1 ║ |
| 49 | +╟───┼───────────────────┼───────────────────╢ |
| 50 | +║ 1 │ foo │ 2 ║ |
| 51 | +╟───┼───────────────────┼───────────────────╢ |
| 52 | +║ 2 │ foo │ 10 ║ |
| 53 | +╟───┼───────────────────┼───────────────────╢ |
| 54 | +║ 3 │ foo │ 60 ║ |
| 55 | +╟───┼───────────────────┼───────────────────╢ |
| 56 | +║ 4 │ foo │ 420 ║ |
| 57 | +╚═══╧═══════════════════╧═══════════════════╝ |
| 58 | +
|
| 59 | +
|
| 60 | + Shape: (5,2) |
| 61 | +
|
| 62 | +╔═══╤═══════════════════╤═══════════════════╗ |
| 63 | +║ │ A │ C_cumprod ║ |
| 64 | +╟───┼───────────────────┼───────────────────╢ |
| 65 | +║ 3 │ foo │ 60 ║ |
| 66 | +╟───┼───────────────────┼───────────────────╢ |
| 67 | +║ 4 │ foo │ 420 ║ |
| 68 | +╟───┼───────────────────┼───────────────────╢ |
| 69 | +║ 5 │ bar │ 3 ║ |
| 70 | +╟───┼───────────────────┼───────────────────╢ |
| 71 | +║ 6 │ bar │ 12 ║ |
| 72 | +╟───┼───────────────────┼───────────────────╢ |
| 73 | +║ 7 │ bar │ 24 ║ |
| 74 | +╚═══╧═══════════════════╧═══════════════════╝ |
| 75 | +``` |
| 76 | + |
| 77 | +Obtain the cumprod for two columns for each groups, group by one column |
| 78 | + |
| 79 | +{% tabs %} |
| 80 | +{% tab title="Node" %} |
| 81 | +```javascript |
| 82 | +const dfd = require("danfojs-node") |
| 83 | + |
| 84 | + |
| 85 | +let data ={'A': ['foo', 'bar', 'foo', 'bar', |
| 86 | + 'foo', 'bar', 'foo', 'foo'], |
| 87 | + 'B': ['one', 'one', 'two', 'three', |
| 88 | + 'two', 'two', 'one', 'three'], |
| 89 | + 'C': [1,3,2,4,5,2,6,7], |
| 90 | + 'D': [3,2,4,1,5,6,7,8] |
| 91 | + } |
| 92 | + |
| 93 | +let df = new dfd.DataFrame(data) |
| 94 | + |
| 95 | +let grp = df.groupby(["A"]) |
| 96 | +let grpCol = grp.col(["C","D"]) |
| 97 | +grpCol.cumProd().print() |
| 98 | +``` |
| 99 | +{% endtab %} |
| 100 | +{% endtabs %} |
| 101 | + |
| 102 | +```text |
| 103 | + |
| 104 | +╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗ |
| 105 | +║ │ A │ C_cumprod │ D_cumprod ║ |
| 106 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 107 | +║ 0 │ foo │ 1 │ 3 ║ |
| 108 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 109 | +║ 1 │ foo │ 2 │ 12 ║ |
| 110 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 111 | +║ 2 │ foo │ 10 │ 60 ║ |
| 112 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 113 | +║ 3 │ foo │ 60 │ 420 ║ |
| 114 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 115 | +║ 4 │ foo │ 420 │ 3360 ║ |
| 116 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 117 | +║ 5 │ bar │ 3 │ 2 ║ |
| 118 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 119 | +║ 6 │ bar │ 12 │ 2 ║ |
| 120 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 121 | +║ 7 │ bar │ 24 │ 12 ║ |
| 122 | +╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝ |
| 123 | +``` |
| 124 | + |
| 125 | +Obtain the cumprod for a column for each group, group by two columns |
| 126 | + |
| 127 | +{% tabs %} |
| 128 | +{% tab title="Node" %} |
| 129 | +```javascript |
| 130 | +const dfd = require("danfojs-node") |
| 131 | + |
| 132 | + |
| 133 | +let data ={'A': ['foo', 'bar', 'foo', 'bar', |
| 134 | + 'foo', 'bar', 'foo', 'foo'], |
| 135 | + 'B': ['one', 'one', 'two', 'three', |
| 136 | + 'two', 'two', 'one', 'three'], |
| 137 | + 'C': [1,3,2,4,5,2,6,7], |
| 138 | + 'D': [3,2,4,1,5,6,7,8] |
| 139 | + } |
| 140 | + |
| 141 | +let df = new dfd.DataFrame(data) |
| 142 | + |
| 143 | +let grp = df.groupby(["A","B"]) |
| 144 | +let grpCol = grp.col(["C"]) |
| 145 | +grpCol.cumProd().print() |
| 146 | + |
| 147 | +``` |
| 148 | +{% endtab %} |
| 149 | +{% endtabs %} |
| 150 | + |
| 151 | +```text |
| 152 | +╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗ |
| 153 | +║ │ A │ B │ C_cumprod ║ |
| 154 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 155 | +║ 0 │ foo │ one │ 1 ║ |
| 156 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 157 | +║ 1 │ foo │ one │ 6 ║ |
| 158 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 159 | +║ 2 │ foo │ two │ 2 ║ |
| 160 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 161 | +║ 3 │ foo │ two │ 10 ║ |
| 162 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 163 | +║ 4 │ foo │ three │ 7 ║ |
| 164 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 165 | +║ 5 │ bar │ one │ 3 ║ |
| 166 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 167 | +║ 6 │ bar │ three │ 4 ║ |
| 168 | +╟────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 169 | +║ 7 │ bar │ two │ 2 ║ |
| 170 | +╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝ |
| 171 | +``` |
| 172 | + |
| 173 | +Obtain the cumprod for two columns for each group, group by two columns |
| 174 | + |
| 175 | +{% tabs %} |
| 176 | +{% tab title="Node" %} |
| 177 | +```javascript |
| 178 | +const dfd = require("danfojs-node") |
| 179 | + |
| 180 | + |
| 181 | +let data ={A: ['foo', 'bar', 'foo', 'bar', |
| 182 | + 'foo', 'bar', 'foo', 'foo'], |
| 183 | + B: ['one', 'one', 'two', 'three', |
| 184 | + 'two', 'two', 'one', 'three'], |
| 185 | + C: [1,3,2,4,5,2,6,7], |
| 186 | + D: [3,2,4,1,5,6,7,8] |
| 187 | +} |
| 188 | + |
| 189 | +let df = new dfd.DataFrame(data) |
| 190 | + |
| 191 | +let grp = df.groupby(["A","B"]) |
| 192 | +let grpCol = grp.col(["C","D"]) |
| 193 | +grpCol.cumProd().print() |
| 194 | +``` |
| 195 | +{% endtab %} |
| 196 | +{% endtabs %} |
| 197 | + |
| 198 | +```text |
| 199 | +
|
| 200 | +╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗ |
| 201 | +║ │ A │ B │ C_cumprod │ D_cumprod ║ |
| 202 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 203 | +║ 0 │ foo │ one │ 1 │ 3 ║ |
| 204 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 205 | +║ 1 │ foo │ one │ 6 │ 21 ║ |
| 206 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 207 | +║ 2 │ foo │ two │ 2 │ 4 ║ |
| 208 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 209 | +║ 3 │ foo │ two │ 10 │ 20 ║ |
| 210 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 211 | +║ 4 │ foo │ three │ 7 │ 8 ║ |
| 212 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 213 | +║ 5 │ bar │ one │ 3 │ 2 ║ |
| 214 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 215 | +║ 6 │ bar │ three │ 4 │ 1 ║ |
| 216 | +╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢ |
| 217 | +║ 7 │ bar │ two │ 2 │ 6 ║ |
| 218 | +╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝ |
| 219 | +
|
| 220 | +``` |
| 221 | + |
0 commit comments