Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Python for Basic Data Analysis: NP.13 Analysing data across arrays

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Comparing 2 arrays

Comparing 2 arrays

You can select elements in an array based on conditions. Let's begin with 2 arrays. 

Analysing data across arrays

np.where

np.where lets you select elements from an array based on conditions. It returns the index of the elements that meets the conditions. 

In this example, using the 2 arrays from above, we can use np.where to find which:

  1. elements that are the same in both arrays
  2. elements in x that are bigger than or equal to its corresponding element in y
  3. elements in x multiplied by 2 that are smaller than its corresponding element in y
  4. elements in y that are bigger than 5


Boolean indexing

Conditional operators like =<, > can be used to compare arrays. Using them will return Boolean values.


In addition, you can use this syntax to return the index of elements that meets the condition:

y [y > 3]



Similarly, you can also use np.where:

Exercises

Use these arrays for the following practice: 

myArray_1 = np.array([2,2,3,4])
myArray_2 = np.array([3,2,3,4])

 

1. Find the elements that are the same in both arrays

2. Find elements in myArray_1 that are smaller than or equal to its corresponding element in myArray_2

3. Find the elements in myArray_1 that is bigger than 2

1. Find the elements that are the same in both arrays


2. Find elements in myArray_1 that are smaller than or equal to its corresponding element in myArray_2


3. Find the elements in myArray_1 that is bigger than 2

Video Guide