larray.Array.append

Array.append(axis, value, label=None) Array[source]

Add a value to this array along an axis.

Parameters
axisaxis reference

Axis along which to append value.

valuescalar or Array

Scalar or array with compatible axes.

labelscalar, optional

Label for the new item in axis. When axis is not present in value, this argument should be used. Defaults to None.

Returns
Array

Array with value appended along axis.

Examples

>>> arr = ones('nat=BE,FO;sex=M,F')
>>> arr["BE", "F"] = 2.0
>>> arr
nat\sex    M    F
     BE  1.0  2.0
     FO  1.0  1.0
>>> sex_total = arr.sum('sex')
>>> sex_total
nat   BE   FO
     3.0  2.0
>>> arr.append('sex', sex_total, label='M+F')
nat\sex    M    F  M+F
     BE  1.0  2.0  3.0
     FO  1.0  1.0  2.0

The value can already have the axis along which it is appended:

>>> sex_total = arr.sum('sex', keepaxes='M+F')
>>> sex_total
nat\sex  M+F
     BE  3.0
     FO  2.0
>>> arr.append('sex', sex_total)
nat\sex    M    F  M+F
     BE  1.0  2.0  3.0
     FO  1.0  1.0  2.0

The value can be a scalar or an array with fewer axes than the original array. In this case, the appended value is expanded (repeated) as necessary:

>>> arr.append('nat', 2, 'Other')
nat\sex    M    F
     BE  1.0  2.0
     FO  1.0  1.0
  Other  2.0  2.0

The value can also have extra axes (axes not present in the original array), in which case, the original array is expanded as necessary:

>>> other = zeros('type=type1,type2')
>>> other
type  type1  type2
        0.0    0.0
>>> arr.append('nat', other, 'Other')
  nat  sex\type  type1  type2
   BE         M    1.0    1.0
   BE         F    2.0    2.0
   FO         M    1.0    1.0
   FO         F    1.0    1.0
Other         M    0.0    0.0
Other         F    0.0    0.0