Ways to Add Row/Columns in Numpy Array - Python
Adding rows or columns to a NumPy array means appending new data along a specific axis. For example, if you have a 2D array like [[1, 2], [3, 4]] and you add a new row [5, 6], the array becomes [[1, 2], [3, 4], [5, 6]]. Similarly, adding a column [7, 8, 9] to a 3x2 array transforms it into a 3x3 array.
Let’s explore different ways to do this efficiently using NumPy.
Add columns in the Numpy array
We can add columns to a NumPy array using append(), concatenate(), insert(), column_stack() or hstack() with axis=1. Just make sure the new column has the same number of rows as the original array.
Using np.append()
np.append() adds values to a NumPy array along a specified axis or flattens if axis is not set.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([[1], [2], [3]])
c = np.append(a, b, axis=1)
print(c)
Output
[[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Explanation:
- a is base array and b is array to append.
- np.append(a, b, axis=1) adds b as a new column to a, resulting in a 3x4 matrix
- whereas, axis=1 append along columns (i.e., horizontally).
Using np.concatenate()
np.concatenate() joins two or more arrays along a specified axis.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([[1], [2], [3]])
c = np.concatenate([a, b], axis=1)
print (c)
Output
[[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Explanation:
- np.concatenate([a, b], axis=1) is joining list of two arrays [a, b].
- axis=1 join along columns (horizontally).
Using np.insert()
np.insert() inserts values into a NumPy array at specified positions along a given axis.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([[1], [2], [3]])
c = np.insert(a, 3, b.flatten(), axis=1)
print(c)
Output
[[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Explanation:
- b.flatten() converts b to a 1D array [1, 2, 3].
- np.insert(a, 3, b.flatten(), axis=1) inserts b as the 4th column (index 3), one value per row.
Using np.hstack()
np.hstack() horizontally stacks arrays (adds columns side by side).
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([1, 2, 3])
c = np.hstack((a, np.atleast_2d(b).T))
print (c)
Output
[[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Explanation:
- np.atleast_2d(b).T converts b to a 3×1 column.
- np.hstack() adds the column to the right of a.
Using np.column_stack()
np.column_stack() stacks 1D or 2D arrays as columns to form a 2D array.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([1, 2, 3])
c = np.column_stack((a, b))
print (c)
Output
[[ 1 2 3 1] [45 4 7 2] [ 9 6 10 3]]
Explanation: np.column_stack((a, b)) stacks b as a new column to the right of a.
Add row in Numpy array
To add rows to a NumPy array, use functions like np.r_, np.insert(), np.vstack() or np.append() with axis=0.
Using np.r_
np.r_ is a shortcut for stacking arrays vertically (row-wise).
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([1, 2, 3])
c = np.r_[a,[b]]
print (c)
Output
[[ 1 2 3] [45 4 7] [ 9 6 10] [ 1 2 3]]
Explanation: np.r_[a, [b]] appends b as a new row at the bottom of a.
Using np.insert()
np.insert() can add rows to a NumPy array by specifying the row index and using axis=0.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([1, 2, 3])
n = a.shape[0]
c = np.insert(a, n, b, axis=0)
print(c)
Output
[[ 1 2 3] [45 4 7] [ 9 6 10] [ 1 2 3]]
Explanation:
- n = a.shape[0] gets row count (3).
- np.insert(a, n, b, axis=0) inserts b as a new row at the end of a.
Using np.vstack()
np.vstack() adds arrays vertically, stacking them as new rows.
import numpy as np
a = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
b = np.array([1, 2, 3])
c = np.vstack ((a, b))
print (c)
Output
[[ 1 2 3] [45 4 7] [ 9 6 10] [ 1 2 3]]
Explanation: np.vstack((a, b)) vertically stacks b as a new row under a.
Using numpy.append()
You can add rows to an empty NumPy array using the numpy.append() function.
Example 1: Adding new rows to an empty 2-D array
import numpy as np
a = np.empty((0,2), int)
print("Empty array:\n", a)
a = np.append(a, np.array([[10,20]]), axis=0)
a = np.append(a, np.array([[40,50]]), axis=0)
print("\nNew array:\n",a )
Output
Empty array: [] New array: [[10 20] [40 50]]
Explanation:
- np.empty((0,2), int) creates an empty array with 0 rows and 2 columns.
- np.append(..., axis=0) appends new rows to a (row-wise), first adds [10, 20] then [40, 50].
Example 2: Adding new rows to an existing 2-D array.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print("Original array:\n", a)
b = np.array([[7, 8, 9], [10, 11, 12]])
c = np.append(a, b, axis=0)
print("\nNew array:\n", c)
Output
Original array: [[1 2 3] [4 5 6]] New array: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
Explanation: a is a 2D array, and b has the same number of columns. Using np.append(axis=0), the new rows are added to the bottom of the original array.
Related Articles: