Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for 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:

Video Guide

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