datatable.sum()¶
Calculate the sum of values for each column from cols. The sum of
the missing values is calculated as zero.
Parameters¶
cols
FExprInput columns.
except
TypeErrorThe exception is raised when one of the columns from cols
has a non-numeric type.
Examples¶
from datatable import dt, f, by
df = dt.Frame({'A': [1, 1, 2, 1, 2],
'B': [None, 2, 3, 4, 5],
'C': [1, 2, 1, 1, 2]})
df
| A | B | C | ||
|---|---|---|---|---|
| int32 | int32 | int32 | ||
| 0 | 1 | NA | 1 | |
| 1 | 1 | 2 | 2 | |
| 2 | 2 | 3 | 1 | |
| 3 | 1 | 4 | 1 | |
| 4 | 2 | 5 | 2 |
Get the sum of column A:
df[:, dt.sum(f.A)]
| A | ||
|---|---|---|
| int64 | ||
| 0 | 7 |
Get the sum of multiple columns:
df[:, [dt.sum(f.A), dt.sum(f.B)]]
| A | B | ||
|---|---|---|---|
| int64 | int64 | ||
| 0 | 7 | 14 |
Same as above, but more convenient:
df[:, dt.sum(f[:2])]
| A | B | ||
|---|---|---|---|
| int64 | int64 | ||
| 0 | 7 | 14 |
In the presence of by(), it returns the sum of the specified columns per group:
df[:, [dt.sum(f.A), dt.sum(f.B)], by(f.C)]
| C | A | B | ||
|---|---|---|---|---|
| int32 | int64 | int64 | ||
| 0 | 1 | 4 | 7 | |
| 1 | 2 | 3 | 7 |
See Also¶
The content on this page is licensed under the Creative Commons Attribution 4.0 License
(CC BY 4.0) ,
and code samples are licensed under the MIT License.