10

Add Row to NumPy Array in Python

 2 years ago
source link: https://thispointer.com/add-row-to-numpy-array-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Add Row to NumPy Array in Python

In this article, we will learn how to add a row to a 2D NumPy Array in python.

Given a NumPy array, we need to add a row to the array. For example,

Example:
Given array:
[[1 2 3 4 5 ],
[5 4 3 2 1 ]]
row = [ 6 7 8 9 1 ]
After adding row to the array:
[[1 2 3 4 5],
[5 4 3 2 1],
[6 7 8 9 1]]
Example:             
	
Given array:

[[1 2 3 4 5 ],
[5 4 3 2 1 ]]

row = [ 6 7 8 9 1 ]

After adding row to the array:

[[1 2 3 4 5],
[5 4 3 2 1],
[6 7 8 9 1]]

There are multiple ways to Add a Row to a NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example

1. Using append() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.append() to append objects to the end of an array, The object should be an array like entity. The append() method will take an array, object to be appended as arguments. It returns a copy of the numpy array, with given values appended at the end.

Syntax of append()

numpy.append(arr, values, axis=None)
numpy.append(arr, values, axis=None)

Parameters:

arr = The array to be passed to the function.
values = array_like object to appended to the array.
axis = int, optional, Axis along which to append values.
arr          = The array to be passed to the function.
values       = array_like object to appended to the array.
axis         = int, optional, Axis along which to append values.

Return:

Returns array with values appended at the end.
Returns array with values appended at the end.

In this case, to add a row to a 2D NumPy array we need to pass the numpy array and row to the append() method and set the axis = 0. It will return a copy of array with the added row.

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
add-row-to-numpy-array-in-python
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0ODI0NmYlNCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMwM2NUYmMDMkN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwNyNDM0ODFwYzY3JzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDtlNDp2MwYkOTUzqWyxPVNyn2yhZG9TUGkurWVlNwImZTQmNDuyYTxlYSZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZuZGQgpz93LXRiLW51oXB5LWFlpzF5LWyhLXB5qGuiovUlRvZzoG9uqFN0YXR1pm1zYWkmZSZynWRmpD1jpzVvnWQ=

Approach

  1. Import numpy library and create a numpy array
  2. Pass the array, row to be added to the append() method and set axis=0.
  3. The append() method will return copy of the array by adding the row.
  4. Print the new array

Source code

import numpy as np
# creating numpy array
arr = np.array([[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1]])
row = np.array([6, 7, 8, 9, 1])
# Adding row to array using append() method
arr = np.append(arr, [row], axis=0)
# Array after adding the row.
print(arr)
import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using append() method
arr = np.append(arr, [row], axis=0)

# Array after adding the row.
print(arr)

OUTPUT:

[[1 2 3 4 5]
[5 4 3 2 1]
[6 7 8 9 1]]
[[1 2 3 4 5]
 [5 4 3 2 1]
 [6 7 8 9 1]]

2. Using concatenate() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.concatenate() to join a sequence of arrays along an existing axis. The concatenate() method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the concatenated array.

Now to Add a Row to a NumPy Array, In the sequence of arrays we will pass the given array and the row to be added, The concatenate() method will return the array with the row added.

Syntax of concatenate()

numpy.concatenate((a1, a2, ...), axis=0)
numpy.concatenate((a1, a2, ...), axis=0)

Parameters:

(a1, a2, ...) = Sequence of arrays to be passed to the function.
axis = int, optional, Axis along which to concatenate arrays.
(a1, a2, ...) = Sequence of arrays to be passed to the function.
axis          = int, optional, Axis along which to concatenate arrays.

Return:

Returns a concatenated array.
Returns a concatenated array.

Approach

  1. Import numpy library and create a numpy array
  2. Now pass the array and row to be added as a sequence of arrays to the concatenate method
  3. The method will return a copy of the array with the row added to it.
  4. Print the new array

Source code

import numpy as np
# creating numpy array
arr = np.array([[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1]])
row = np.array([6, 7, 8, 9, 1])
# Adding row to array using concatenate()
arr = np.concatenate([arr, [row]])
# Array after adding the row.
print(arr)
import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using concatenate()
arr = np.concatenate([arr, [row]])

# Array after adding the row.
print(arr)

OUTPUT:

[[1 2 3 4 5]
[5 4 3 2 1]
[6 7 8 9 1]]
[[1 2 3 4 5]
 [5 4 3 2 1]
 [6 7 8 9 1]]

3. Using insert() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.insert() to insert values along the given axis before the given index. The insert() method will take an array, index , values to be inserted as parameters. It will insert the given value just before the specified index and returns the array.

Now, to Add a Row to a NumPy Array we need to pass the array, index, row to be inserted to the insert() method. Here we are adding row at front of the array so let’s give index = 0.

Syntax of insert()

numpy.insert(arr, obj, values, axis=None)
numpy.insert(arr, obj, values, axis=None)

Parameters:

arr = The array to be passed to the function.
obj = index at which value needs to be inserted
values = Values or object to insert into array.
axis = int, optional, Axis along which to insert values.
arr          = The array to be passed to the function.
obj          = index at which value needs to be inserted
values       = Values or object to insert into array.
axis         = int, optional, Axis along which to insert values.

Return:

Returns array with value inserted at the specified index, in this case appended at the end of the array.
Returns array with value inserted at the specified index, in this case appended at the end of the array.

Approach

  • Import numpy library and create numpy array
  • Now pass the array, row to be inserted and index = 0, axis = 0 to the insert() method
  • That’s it , The insert() method will return a copy of the array with the row added.
  • Print the new array.

Source code

import numpy as np
# creating numpy array
arr = np.array([[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1]])
row = np.array([6, 7, 8, 9, 1])
# Adding row to array using insert()
arr = np.insert(arr, 0, row, axis=0)
# Array after adding the row.
print(arr)
import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6, 7, 8, 9, 1])
        
# Adding row to array using insert()
arr = np.insert(arr, 0, row, axis=0)

# Array after adding the row.
print(arr)

OUTPUT:

[[6 7 8 9 1]
[1 2 3 4 5]
[5 4 3 2 1]]
[[6 7 8 9 1]
 [1 2 3 4 5]
 [5 4 3 2 1]]

4. Using vstack() method to Add a Row to a NumPy Array

Numpy module in python, provides a function numpy.vstack() function is used to Stack arrays in sequence vertically (row-wise). i.e, concatenating into a single array. The vstack() method will take a sequence of arrays as parameters. It will stack the arrays into one single array and returns the array. The vstack is equivalent to concatenation.

Now to Add a Row to a NumPy Array, In the sequence of arrays we will pass the given array and the row to be added, The vstack() method will return the array with the row added.

Syntax of vstack()

numpy.vstack(tuple)
numpy.vstack(tuple)

Parameters:

tuple = sequence of arrays to be passed to the function.
tuple = sequence of arrays to be passed to the function.

Return:

Returns The array formed by stacking the given arrays.
Returns The array formed by stacking the given arrays.

Approach

  • Import numpy library and create numpy array
  • Now pass the array, row to be inserted as a sequence of arrays to the vstack method
  • That’s it , The vstack() method will return a copy of the array with the row added.
  • Print the new array.

Source code

import numpy as np
# creating numpy array
arr = np.array([[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1]])
row = np.array([6,7,8,9,1])
# Adding row to array using vstack()
arr = np.vstack((arr,row))
# Array after adding the row.
print(arr)
import numpy as np

# creating  numpy array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1]])

row = np.array([6,7,8,9,1])

# Adding row to array using vstack()
arr = np.vstack((arr,row))

# Array after adding the row.
print(arr)

OUTPUT:

[[1 2 3 4 5]
[5 4 3 2 1]
[6 7 8 9 1]]
[[1 2 3 4 5]
 [5 4 3 2 1]
 [6 7 8 9 1]]

Summary

Great! you made it, We have discussed all possible methods to Add a Row to a NumPy Array. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK