Home > Software engineering >  How to take max value from from certain numbers in a row of a numpy array using input?
How to take max value from from certain numbers in a row of a numpy array using input?

Time:11-23

I have an excel file that looks something like this:

code boys girls food
1224 485 540 582
1679 398 423 391
9626 23 25 58
9806 150 83 120

I have an input that asks for a code, and prints the max value of the row not including the code. I was thinking of using the input and finding the index of it, but I am stuck

import numpy as np
import matplotlib.pyplot as plt
    
numbers = np.genfromtxt('numbers.csv', delimiter=',', skip_header=True)

choiceCode = input('Enter a code')    
Row = numbers[choiceCode] #I tried to find the index of the input given

print(np.max(Row[1:4]))   ##then tried finding max of it

CodePudding user response:

One way to do this is to use np.where. If you google search for 'how to find a value in numpy array' you will find this and some other ways.

numbers = np.array([
       [1224.,  485.,  540.,  582.],
       [1679.,  398.,  423.,  391.],
       [9626.,   23.,   25.,   58.],
       [9806.,  150.,   83.,  120.]
])

choiceCode = 9626
i = np.where(numbers[:, 0] == choiceCode)[0]  # take first match
if i.size > 0:
    print(np.max(numbers[i, 1:]))

Output:

58.0
  • Related