7

Add elements to the end of Array in Python

 2 years ago
source link: https://thispointer.com/add-elements-to-the-end-of-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.
neoserver,ios ssh client

Add elements to the end of Array in Python

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

Given a NumPy array we need to add an element to the end of NumPy Array i.e. add an element at the last index of
the array.

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

Given array = [ 1, 3, 5, 8, 9 ]
After adding 10 to the end of NumPy Array = [ 1, 3, 5, 8, 9, 10 ]
    

There are multiple ways to add an element to the end of NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example,

1. Using insert() method to add an element to the end of NumPy Array

Numpy module in python, provides a function numpy.insert() to insert values along the given axis and before the given index. The insert() method will take array, index and a value to be inserted as parameters. It will insert the given value just before the specified index and returns the array. Now, to add a element at the end of numpy array we need to pass the array, the length of array as index position and value to the insert() method i.e. insert( arr, len(arr) , 6).

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 to be inserted 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 to be inserted into array.
axis         = int, optional, Axis along which to insert values.

Return:

Returns array with value inserted at specified index.
Returns array with value inserted at specified index.

Approach

1. Import numpy library and create numpy array
2. Now pass the array, value to be inserted and index as the length of the array to the insert() method
3. That’s it , the element will be added at specified index i.e. end of the array
4. Print the array.

Advertisements

Source code

import numpy as np
# create numpy array
arr = np.array([1,2,3,4,5])
# printing the original array
print(" The original array is = ", arr)
#adding an element at end of array using insert() method
arr = np.insert(arr,len(arr) , 6)
# printing the new array
print(" The new array is = ", arr)
import numpy as np

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

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

#adding an element at end of array using insert() method
arr = np.insert(arr,len(arr) , 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 = [1 2 3 4 5 6]
 The original array is =  [1 2 3 4 5]
 The new array is =  [1 2 3 4 5 6]

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

Numpy module in python, provides a function numpy.append() to append values to the end of an array. The insert 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)
numpy.append(arr, values, axis=None)

Parameters:

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

Return:

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

Approach

1. import numpy library and create numpy array
2. Now pass the array, value to be appended to the array to the append() method.
3. That’s it. The element will be appended at the end of array
4. Print the array

Source code

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

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

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

#appending an element at end of array using append() method
arr = np.append(arr,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 = [1 2 3 4 5 6]
 The original array is =  [1 2 3 4 5]
 The new array is =  [1 2 3 4 5 6]

3. Using concatenate() method to add an element to the end 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 sequence of arrays as parameters. It concatenates the arrays into one single array and returns the array.

Now to append a element at end of an array, create a sequence of array and add element to sequence. Then concatenate both the arrays.

Example

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

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 numpy array
2. Now pass the array, value to be added as sequence of array to the concatenate() method
3. That’s it. The element will be appended at the end of array
4. Print the array

Source code

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

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

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

#adding an element at end of array using concatenate() method
arr = np.concatenate((arr,[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 = [1 2 3 4 5 6]
 The Original array is =  [1 2 3 4 5]
 The new array is =  [1 2 3 4 5 6]

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

Numpy module in python, provides a function numpy.hstack() and it 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.

Now to append a element at end of an array, create a sequence of array and add element to sequence. Then concatenate both the arrays.

Example

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

Syntax of hstack()

numpy.hstack(tuple)
numpy.hstack(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

1. import numpy library and create numpy array
2. Now pass the array, value to be added as sequence of array to the hstack() method
3. Thats it , The element will be appended at the end of array
4. Print the array

Source code

# importing the numpy library
import numpy as np
# create numpy array
arr = np.array([1,2,3,4,5])
# printing the original array
print(" The original array is = ", arr)
arr = np.hstack((arr, [6]))
# printing the new array
print(" The new array is = ", arr)
# importing the numpy library
import numpy as np

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

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

arr = np.hstack((arr, [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 = [1 2 3 4 5 6]
 The original array is =  [1 2 3 4 5]
 The new array is =  [1 2 3 4 5 6]

Summary

We learned to how to insert elements at the end of a NumPy array in Python.

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