Categories: NumpyPython

Using conditional statements to select parts of NumPy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

In addition to simply using indexing to select elements from a Numpy array, Python lets you easily use conditional tests to systematically select parts of a Numpy array. In the following example, elements less than 0.8 from a two-dimensional array is selected via a conditional test inside the brackets, and the returning result is a one-dimensional array.

#import Numpy module
import numpy as np
#create an array of shape 3,2
T = np.random.random((3, 2))
T
#output
array([[0.39818887, 0.09905403],
       [0.62459552, 0.34570658],
       [0.25420698, 0.9049677 ]])
#select element of array, with condition less than 0.8
T[T < 0.8]
#result
array([0.39818887, 0.09905403, 0.62459552, 0.34570658, 0.25420698])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago