datatable.sum()

Calculate the sum of values for each column from cols. The sum of the missing values is calculated as zero.

Parameters

cols
Expr

Input columns.

return
Expr

f-expression having one row, and the same names and number of columns as in cols. The column stypes are int64 for boolean and integer columns, float32 for float32 columns and float64 for float64 columns.

except
TypeError

The 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
ABC
int32int32int32
01NA1
1122
2231
3141
4252

Get the sum of column A:

df[:, dt.sum(f.A)]
A
int64
07

Get the sum of multiple columns:

df[:, [dt.sum(f.A), dt.sum(f.B)]]
AB
int64int64
0714

Same as above, but more convenient:

df[:, dt.sum(f[:2])]
AB
int64int64
0714

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)]
CAB
int32int64int64
0147
1237

See Also

  • count() – function to calculate a number of non-missing values.