Articles

Remove first element from numpy Array using methods slicing, delete() & boolean array

Remove first element from numpy Array using methods slicing, delete() & boolean array


In this numpy tutorial, we will discuss about:

  • remove first element from numpy array python using slicing
  • remove first element from numpy array python using delete()
  • remove first element from numpy array python using boolean array

Before understanding about remove first element from numpy array python, lets know about numpy and array.

 

numpy stands for numeric python which is used to perform mathematical operations on arrays.

 

It is a module in which we have to import from the python

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.


Array

An array is an one dimensional data structure used to store single data type data. Array will only store all integer data or all string type data.or all float type data.

 

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.


Problem Scenario:

Input- [12,34,32,56,78,90,65]

Output- [34,32,56,78,90,65]

So all we need to do is remove first element from array numpy.

 

We will see two methods of achieving our desired result of remove first element from array numpy.


Method 1remove first element from array numpy using Slicing

Here we will use slicing, to remove first element from array numpy.

 

first element position is 0 th index. So by slicing from 1st index, we can ignore first element.

Syntax:

array_input[1:]

where, array_input is the input numpy array.

 

Exampleremove first element from array numpy

In this remove first element from array numpy example, we will remove first element through slicing

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type
array_data=numpy.array([34,56,43,22,45,6,54,2])

#display the actual numpy array
print(array_data)

#remove first element using slicing
print(array_data[1:])

Output: From the above np array remove first element code, we created numpy array with 8 elements. First element is 34, so we will exclude this element and display the actual and modified numpy array.

[34, 56, 43, 22, 45, 6, 54, 2]
[56, 43, 22, 45, 6, 54, 2]

Now lets see another method for np array remove first element.


Method 2remove first element from array numpy using delete()

Here we will use delete() method available in numpy module to remove first element from array numpy.

 

first element position is 0th index. We can delete the first element by specifying the index.

Syntax:

numpy.delete(array_input, 0)

where, array_input is the input numpy array and 0 represents the first element index

 

Exampleremove first element from array numpy

In this remove first element from array numpy example, we will remove first element using delete() method.

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type
array_data=numpy.array([34,56,43,22,45,6,54,2])

#display the actual numpy array
print(array_data)

#remove first element using delete()
print(numpy.delete(array_data, 0))

OutputFrom the above np array remove first element code, we created numpy array with 8 elements. First element is 34, so we will exclude this element and display the actual and modified numpy array.

[34, 56, 43, 22, 45, 6, 54, 2]
[56, 43, 22, 45, 6, 54, 2]

Lets see one more method for np array remove first element.


Method 3remove first element from numpy array python Using Boolean Array

Here we are using Boolean Array that will take two boolean values - True and False.

 

So we can use these values which act as index for the actual numpy array.

  1. False - will take no value
  2. True - will take value

By specifying the False value in the first position of the boolean array, we can exclude the first value from the numpy array.

Through this boolean array , we can index through actual array.

Syntax:

array_input[boolean_array]

Exampleremove first element from numpy array python

In this remove first element from numpy array python example, we will remove first element using boolen array indexing.

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type
array_data=numpy.array([34,56,43,22,45,6,54,2])

#create the boolean array
b_array=[False, True, True, True, True, True, True,True]

#display the actual numpy array
print(array_data)

#remove first element through indexing boolean array
print(array_data[b_array])

OutputFrom the above np array remove first element code, we created numpy array with 8 elements. First element is 34, so we will exclude this element and display the actual and modified numpy array.

[34 56 43 22 45  6 54  2]
[56 43 22 45  6 54  2]

Thus we wrap up our session on remove first element from array numpy.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!