NumPy arrays often arrive as matching blocks, such as monthly rows, feature columns, or batches from separate files. Concatenation joins those blocks along an axis that already exists so later calculations can work with one array.

The selected axis is the dimension that grows. For a two-dimensional array, axis=0 adds rows while preserving the column count, and axis=1 adds columns while preserving the row count.

Use np.concatenate() only when the dimensions outside the join axis already match. When each input should remain a separate layer, use np.stack() instead; when axis=None is used, NumPy flattens each input before joining.

Related: Stack arrays
Related: Create an array
Related: Reshape an array

Steps to concatenate NumPy arrays:

  1. Create a script that joins row-compatible and column-compatible arrays.
    array-concatenate.py
    import numpy as np
     
    north = np.array([[10, 11, 12], [13, 14, 15]])
    south = np.array([[20, 21, 22]])
    east = np.array([[30], [31]])
     
    by_row = np.concatenate((north, south), axis=0)
    by_column = np.concatenate((north, east), axis=1)
     
    print("north shape:", north.shape)
    print("south shape:", south.shape)
    print("east shape:", east.shape)
    print("by row:")
    for row in by_row:
        print(" ", row.tolist())
    print("by row shape:", by_row.shape)
    print("by column:")
    for row in by_column:
        print(" ", row.tolist())
    print("by column shape:", by_column.shape)

    axis=0 grows the first dimension, so south must have three columns. axis=1 grows the second dimension, so east must have two rows.

  1. Run the script and verify the joined shapes.
    $ python3 array-concatenate.py
    north shape: (2, 3)
    south shape: (1, 3)
    east shape: (2, 1)
    by row:
      [10, 11, 12]
      [13, 14, 15]
      [20, 21, 22]
    by row shape: (3, 3)
    by column:
      [10, 11, 12, 30]
      [13, 14, 15, 31]
    by column shape: (2, 4)

    The output keeps rank 2 in both joins. The row join becomes (3, 3), and the column join becomes (2, 4).

  1. Test a mismatched non-joined dimension before using new inputs.
    $ python3 - <<'PY'
    import numpy as np
    
    left = np.array([[1, 2], [3, 4]])
    bad = np.array([[5, 6, 7]])
    
    try:
        np.concatenate((left, bad), axis=0)
    except ValueError as error:
        print(f"{type(error).__name__}: {error}")
    PY
    ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 3

    Do not reshape around this error until the row or column meaning is confirmed. A forced reshape can align dimensions while mixing unrelated observations.