larray.Array.with_total

Array.with_total(*args, op=sum, label='total', **kwargs)[source]

Add aggregated values (sum by default) along each axis. A user defined label can be given to specified the computed values.

Parameters:
*args : int or str or Axis or Group or any combination of those, optional

Axes or groups along which to compute the aggregates. Passed groups should be named. Defaults to aggregate over the whole array.

op : aggregate function, optional

Available aggregate functions are: sum, prod, min, max, mean, ptp, var, std, median and percentile. Defaults to sum.

label : scalar value, optional

Label to use for the total. Applies only to aggregated axes, not groups. Defaults to “total”.

**kwargs : int or str or Group or any combination of those, optional

Axes or groups along which to compute the aggregates.

Returns:
Array

Examples

>>> arr = ndtest("gender=M,F;time=2013..2016")
>>> arr
gender\time  2013  2014  2015  2016
          M     0     1     2     3
          F     4     5     6     7
>>> arr.with_total()
gender\time  2013  2014  2015  2016  total
          M     0     1     2     3      6
          F     4     5     6     7     22
      total     4     6     8    10     28

Using another function and label

>>> arr.with_total(op=mean, label='mean')
gender\time  2013  2014  2015  2016  mean
          M   0.0   1.0   2.0   3.0   1.5
          F   4.0   5.0   6.0   7.0   5.5
       mean   2.0   3.0   4.0   5.0   3.5

Specifying an axis and a label

>>> arr.with_total('gender', label='U')
gender\time  2013  2014  2015  2016
          M     0     1     2     3
          F     4     5     6     7
          U     4     6     8    10

Using groups

>>> time_groups = (arr.time[:2014] >> 'before_2015',
...                arr.time[2015:] >> 'after_2015')
>>> arr.with_total(time_groups)
gender\time  2013  2014  2015  2016  before_2015  after_2015
          M     0     1     2     3            1           5
          F     4     5     6     7            9          13
>>> # or equivalently
>>> # arr.with_total('time[:2014] >> before_2015; time[2015:] >> after_2015')