1

Insert an element at the beginning of NumPy Array

 2 years ago
source link: https://thispointer.com/insert-an-element-at-the-beginning-of-numpy-array/
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.

Insert an element at the beginning of NumPy Array

In this article, we will learn how to add an element to the front of NumPy Array in Python.

Given a NumPy array, we need to add an element to the front of NumPy Array i.e. insert an element at the 0th index position of the array.

Example:
Given array:
[1, 3, 5, 8, 9]
After adding 10 to the front of NumPy Array:
[10, 1, 3, 5, 8, 9]
Example:             

Given array:
[1, 3, 5, 8, 9]

After adding 10 to the front of NumPy Array:
[10, 1, 3, 5, 8, 9]

There are multiple ways to add an element to the front of NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example

1.) Using insert() to add an element to the front of 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 array, index and value to be inserted as parameters. It will insert the given value just before the specified index in a copy of array and returns the modified array copy.

Now, to add a element at the front of numpy array we need to pass the array, index as 0 and value to the insert() method i.e. insert(arr,0 , 6).

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
00:00/15:21
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0ODI0NmU3MCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMwM2NUYmMDMkN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwNyNDMkMTtjNmxjJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDtlNDp1NmEmNTMzqWyxPVNyn2yhZG9TUGkurWVlNwImZTQmMTI1MWEjMlZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZcoaNypaQgYW4gZWkyoWVhqC1uqC10nGUgYzVanW5hnW5aLW9zLW51oXB5LWFlpzF5JTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==

Syntax of insert()

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 to insert into array.
axis = int, optional, Axis along which to insert values.
Returns:
Returns array with value inserted at the specified index, in this case, appended at the front of the array.
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 to insert into array.
	axis         = int, optional, Axis along which to insert values.

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

Approach

  • Import numpy library and create numpy array
  • Now pass the array, value to be inserted and set index = 0 to the insert() method
  • The insert() method will return a copy of the array by adding the element at the front of the array
  • Print the array.

Source code

import numpy as np
# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The Original array is = ", arr)
#adding an element at front of array using insert() method
arr = np.insert(arr, 0, 6)
# printing the new array
print(" The new array is = ", arr)
import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

#adding an element at front of array using insert() method
arr = np.insert(arr, 0, 6)

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

The Original array is = [1 2 3 4 5]
The new array is = [6 1 2 3 4 5]
 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

2.) Using append() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.append() to append values to the end of an array. The append method will take array, value to be appended as parameters. It will append the given value at the End of the array and returns the array.

Syntax of append()

numpy.append(arr, values, axis=None)
Parameters:
arr = The array to be passed to the function.
values = Values to be appended to array.
axis = int, optional, Axis along which to append values.
Returns:
Returns array with values appended at the end.
numpy.append(arr, values, axis=None)

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

Returns:
	Returns array with values appended at the end.

Now in order to append the element at front of the array, we need to pass the element to be appended first and then pass the array to the append() method.

Approach

  • Import numpy library and create numpy array
  • Now pass the value to be appended and array to the append() method.
  • The append method will return a copy of the array by adding the element at the front of the array
  • Print the array

Source code

import numpy as np
# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The Original array is = ", arr)
#appending an element at front of the array using append() method
arr = np.append(6, arr)
# printing the new array
print(" The new array is = ", arr)
import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

#appending an element at front of the array using append() method
arr = np.append(6, arr)

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

The Original array is = [1 2 3 4 5]
The new array is = [6 1 2 3 4 5]
 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

3.) Using concatenate() to add an element to the front of 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 array.

Syntax of concatenate()

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.
Returns:
Returns a concatenated array.
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.

Returns:
	Returns a concatenated array.

Now to append an element at front of an array. We need to pass the element to be appended first and then pass the array to the concatenate() method. It will return a copy of the array with the element added at the front.

Example

concatenate(([6] , [1,2,3,4,5] )) ==> [6, 1, 2, 3, 4, 5]

Approach

  • Import numpy library and create numpy array.
  • Now pass the value to be appended and array as sequence of array to the concatenate method.
  • The concatenate() method will return a copy of array by adding the element at the front of array.
  • Print the array.

Source code

import numpy as np
# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The Original array is = ", arr)
# Adding an element at front of array using concatenate() method
arr = np.concatenate(([6], arr))
# printing the new array
print(" The new array is = ", arr)
import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

# Adding an element at front of array using concatenate() method
arr = np.concatenate(([6], arr))

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

The Original array is = [1 2 3 4 5]
The new array is = [6 1 2 3 4 5]
 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

4.) Using hstack() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.hstack() function is used to stack the sequence of input arrays horizontally i.e, concatenating into a single array. The hstack() method will take sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the array. The hstack() is equivalent to concatenation.

Syntax of hstack()

numpy.hstack(tuple)
Parameters:
tuple = sequence of arrays to be passed to the function.
Returns:
Returns The array formed by stacking the given arrays.
numpy.hstack(tuple)

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

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

Now to append an element at front of an array, In the sequence of array, we need to pass the element to be appended first and then the array to the hstack() method. It will return a copy of the array with the element added at the front.

Example

hstack( ([6], [1,2,3,4,5]) ) ==> [6,1,2,3,4,5]

Approach

  • Import numpy library and create numpy array
  • Now pass the value to be appended and array as sequence of array to the hstack() method
  • The hstack() method will return a copy of array by adding the element at the front of array
  • Print the array

Source code

import numpy as np
# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The Original array is = ", arr)
# Adding an element at front of array
arr = np.hstack(([6], arr))
# printing the new array
print(" The new array is = ", arr)
import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

# Adding an element at front of array
arr = np.hstack(([6], arr))

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

The Original array is = [1 2 3 4 5]
The new array is = [6 1 2 3 4 5]
 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

Summary

Great! you made it, We have discussed all possible methods to add an element to the front of NumPy Array.

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