Wednesday, July 3, 2024
HomeLanguagesPythonFind the position of number that is multiple of certain number

Find the position of number that is multiple of certain number

In this type of question, a number is given and we have to find the position or index of all multiples of that number. For doing this question we use a function named numpy.argwhere().

Syntax:

numpy.argwhere(array)

Example 1: 

Sometimes we need to find the indexes of the element that are divisible by int or float number.

Python3




# Importing Pandas and Numpy libraries
import pandas as pd
import numpy as np
  
# Creating a Series of random numbers
n_series = pd.Series(np.random.randint(1, 25, 15))
print("Original Series:\n")
print(n_series)
  
# Finding the indexes of numbers divisible by 3
res_index = np.argwhere(n_series % 3==0)
print("Positions of numbers that are multiples of 3:\n")
print(res_index)


Output:

In the above example, we find the indexes of all numbers divisible by 3.

Example 2:

Python3




# Importing Pandas and Numpy libraries
import pandas as pd
import numpy as np
  
# Creating a Series of random numbers
n_series = pd.Series(np.random.randint(1, 20, 10))
print("Original Series:\n")
print(n_series)
  
# Finding the indexes of numbers divisible by 3.5
res_index = np.argwhere(n_series % 3.5==0)
print("Positions of numbers that are multiples of 3.5:\n")
print(res_index)


Output:

In the above example, we find the indexes of all numbers divisible by floating-point number 3.5

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments