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 ]])
print ( "initial_array : " , str (ini_array));
column_to_be_added = np.array([[ 1 ], [ 2 ], [ 3 ]])
arr = np.append(ini_array, column_to_be_added, axis = 1 )
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 ]])
column_to_be_added = np.array([[ 1 ], [ 2 ], [ 3 ]])
arr = np.concatenate([ini_array, column_to_be_added], axis = 1 )
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 ]])
column_to_be_added = np.array([[ 1 ], [ 2 ], [ 3 ]])
arr = np.insert(ini_array, 0 , column_to_be_added, axis = 1 )
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 ]])
column_to_be_added = np.array([ 1 , 2 , 3 ])
result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))
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 ]])
column_to_be_added = np.array([ 1 , 2 , 3 ])
result = np.column_stack((ini_array, column_to_be_added))
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 ]])
print ( "initial_array : " , str (ini_array));
row_to_be_added = np.array([ 1 , 2 , 3 ])
result = np.r_[ini_array,[row_to_be_added]]
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 ]])
row_to_be_added = np.array([ 1 , 2 , 3 ])
row_n = arr.shape[ 0 ]
arr = np.insert(ini_array,row_n,[row_to_be_added],axis = 0 )
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 ]])
row_to_be_added = np.array([ 1 , 2 , 3 ])
result = np.vstack ((ini_array, row_to_be_added) )
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
import numpy as np
empt_array = np.empty(( 0 , 2 ), int )
print ( "Empty array:" )
print (empt_array)
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
import numpy as np
empt_array = np.empty(( 0 , 3 ), int )
print ( "Empty array:" )
print (empt_array)
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
import numpy as np
empt_array = np.empty(( 0 , 4 ), int )
print ( "Empty array:" )
print (empt_array)
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 di
3 min read
How to swap columns of a given NumPy array?
In this article, let's discuss how to swap columns of a given NumPy array. Approach : Import NumPy moduleCreate a NumPy arraySwap the column with IndexPrint the Final array Example 1: Swapping the column of an array. C/C++ Code # importing Module import numpy as np # creating array with shape(4,3) m
2 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
Convert Python List to numpy Arrays
NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
Python - Boolean Array in NumPy
In this article, I'll be explaining how to generate boolean arrays in NumPy and utilize them in your code. In NumPy, boolean arrays are straightforward NumPy arrays with array components that are either "True" or "False." Note: 0 and None are considered False and everything else is considered True.
3 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 (
2 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