Ways to Add Row/Columns in Numpy Array - Python
Last Updated : 24 Jun, 2025
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.
Python 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.
Python 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.
Python 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).
Python 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.
Python 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).
Python 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.
Python 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.
Python 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
Python 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 )
OutputEmpty 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.
Python 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)
OutputOriginal 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:
Similar Reads
Python | Ways to add row/columns in numpy array 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 arr
5 min read
Ways to Convert a Python Dictionary to a NumPy Array The task of converting a dictionary to a NumPy array involves transforming the dictionaryâs key-value pairs into a format suitable for NumPy. In Python, there are different ways to achieve this conversion, depending on the structure and organization of the resulting array.For example, consider a dic
3 min read
Different Ways to Create Numpy Arrays in Python Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create
3 min read
numpy.column_stack() in Python numpy.column_stack() function is used to stack 1-D arrays as columns into a 2-D array.It takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack function. Syntax : numpy.column_stack(tup) Parameters : tup : [sequence of
2 min read
Python | Numpy numpy.ndarray.__add__() With the help of Numpy numpy.ndarray.__add__(), we can add a particular value that is provided as a parameter in the ndarray.__add__() method. Value will be added to each and every element in a numpy array. Syntax: ndarray.__add__($self, value, /) Return: self+value Example #1 : In this example we c
1 min read
How to Swap Two Rows in a NumPy Array One common task you might encounter when working with NumPy arrays is the need to swap two rows. Swapping rows can be essential in data preprocessing, reshaping data, or reordering data to perform specific analyses in Python. In this article, we will explore different methods to swap two rows in a N
4 min read