0

I am using Python 3 on CentOS 7. I would like to find the quickest way to get the index of the array with the maximum, for each index with the arrays, over multiple arrays without using loops. For example, if I input

array[0] = [1,3,9,4,6,8,9] array[1] = [2,6,3,8,7,4,5] array[2] = [6,3,7,9,1,3,6]

I would like the output to be

[2,1,0,2,1,0,0]

I tried

np.maximum.reduce(array)

and got the maximum values, across the arrays, for the indices across arrays. However, when I tried

array.index(np.maximum.reduce(array))

I get

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

2 Answers 2

2

Try with argmax from numpy

np.argmax(array, axis = 1)

output

[2 3 3]
Sign up to request clarification or add additional context in comments.

1 Comment

That worked, except I had to use axis = 0. Thank very much!
1

Try this:

import numpy as np
import pandas as pd
arr = np.array([[1,3,9,4,6,8,9],
        [2,6,3,8,7,4,5],
        [6,3,7,9,1,3,6]])

df = pd.DataFrame(arr)

print(df)

result = [df[x].idxmax() for x in df]
print(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.