Python | Ways to add row/columns in numpy array
Last Updated :
22 Mar, 2023
Given a Numpy array, the task is to add rows/columns basis on requirements to the Numpy array. Let's see a few examples of this problem in Python.
Add columns in the Numpy array
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array));
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.append(ini_array, column_to_be_added, axis=1)
# printing result
print ("resultant array", str(arr))
Output:
initial_array : [[ 1 2 3]
[45 4 7]
[ 9 6 10]]
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.concatenate([ini_array, column_to_be_added], axis=1)
# printing result
print ("resultant array", str(arr))
Output:
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.insert(ini_array, 0, column_to_be_added, axis=1)
# printing result
print ("resultant array", str(arr))
Output:
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))
# printing result
print ("resultant array", str(result))
Output:
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.column_stack((ini_array, column_to_be_added))
# printing result
print ("resultant array", str(result))
Output:Â
resultant array [[ 1 2 3 1]
[45 4 7 2]
[ 9 6 10 3]]
Add row in Numpy array
Method 1: Using np.r_Â
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array));
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.r_[ini_array,[row_to_be_added]]
# printing result
print ("resultant array", str(result))
Output:
initial_array : [[ 1 2 3]
[45 4 7]
[ 9 6 10]]
resultant array [[ 1 2 3]
[45 4 7]
[ 9 6 10]
[ 1 2 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
#last row
row_n = arr.shape[0]
arr = np.insert(ini_array,row_n,[row_to_be_added],axis= 0)
# printing result
print ("resultant array", str(arr))
Output:
resultant array [[ 1 2 3]
[45 4 7]
[ 9 6 10]
[ 1 2 3]]
Python3
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.vstack ((ini_array, row_to_be_added) )
# printing result
print ("resultant array", str(result))
Output:
resultant array [[ 1 2 3]
[45 4 7]
[ 9 6 10]
[ 1 2 3]]
Sometimes we have an empty array and we need to append rows in it. Numpy provides the function to append a row to an empty Numpy array using numpy.append() function.
Example 1: Adding new rows to an empty 2-D array
Python3
# importing Numpy package
import numpy as np
# creating an empty 2d array of int type
empt_array = np.empty((0,2), int)
print("Empty array:")
print(empt_array)
# adding two new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10,20]]), axis=0)
empt_array = np.append(empt_array, np.array([[40,50]]), axis=0)
print("\nNow array is:")
print(empt_array)
Empty array:
[]
Now array is:
[[10 20]
[40 50]]
Example 2: Adding new rows to an empty 3-D array
Python3
# importing Numpy package
import numpy as np
# creating an empty 3d array of int type
empt_array = np.empty((0,3), int)
print("Empty array:")
print(empt_array)
# adding three new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10,20,40]]), axis=0)
empt_array = np.append(empt_array, np.array([[40,50,55]]), axis=0)
empt_array = np.append(empt_array, np.array([[40,50,55]]), axis=0)
print("\nNow array is:")
print(empt_array)
Empty array:
[]
Now array is:
[[10 20 40]
[40 50 55]
[40 50 55]]
Example 3: Adding new rows to an empty 4-D array
Python3
# importing Numpy package
import numpy as np
# creating an empty 4d array of int type
empt_array = np.empty((0,4), int)
print("Empty array:")
print(empt_array)
# adding four new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[100,200,400,888]]), axis=0)
empt_array = np.append(empt_array, np.array([[405,500,550,558]]), axis=0)
empt_array = np.append(empt_array, np.array([[404,505,555,145]]), axis=0)
empt_array = np.append(empt_array, np.array([[44,55,550,150]]), axis=0)
print("\nNow array is:")
print(empt_array)
Empty array:
[]
Now array is:
[[100 200 400 888]
[405 500 550 558]
[404 505 555 145]
[ 44 55 550 150]]
Similar Reads
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
numpy.char.add() function in Python The add() method of the char class in the NumPy module is used for element-wise string concatenation for two arrays of str or unicode. numpy.char.add()Syntax : numpy.char.add(x1, x2)Parameters : x1 : first array to be concatenated (concatenated at the beginning)x2 : second array to be concatenated (
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
Python | Numpy numpy.ndarray.__iadd__() With the help of numpy.ndarray.__iadd__() method, we can add a particular value that is provided as a parameter in the ndarray.__iadd__() method. Value will be added to every element in a numpy array. Syntax: ndarray.__iadd__($self, value, /) Return: self+=value Example #1 : In this example we can s
1 min read
NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
2 min read
Python Lists VS Numpy Arrays Here, we will understand the difference between Python List and Python Numpy array. What is a Numpy array?NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operati
7 min read
Find the sum and product of a NumPy array elements In this article, let's discuss how to find the sum and product of NumPy arrays. Sum of the NumPy array Sum of NumPy array elements can be achieved in the following ways Method #1:  Using numpy.sum() Syntax: numpy.sum(array_name, axis=None, dtype=None, out=None, keepdims=<no value>, initial=
5 min read