1

Given I have four lists (in reality I have many more, read in from a CSV as numpy arrays) such that:

a = [54,35,67...]
b = [45,21,87...]
c = [32,58,52...]
d = [4,78,43...]
# In reality I have plant % cover surveyed at a location

and four lists like:

A = ['foo', 'bar', 'ham'...]
B = ['eggs', 'spam', 'bar'...]
C = ['ham', 'eggs', 'foo'...]
D = ['eggs', 'spam', 'bar'...]
# These are plant species

I can find the max of a, b, c and d using:

max = [a, b, c, d].max(axis=0)
# The max plant % cover between a, b, c and d on the 0 axis

to get:

max = [54, 78, 87...]
# the plant species that accompanies the max plant % cover

but how do I now get the corresponding text value, so my output looks like:

Text = ['foo', 'spam', 'bar'...]
3
  • Just to clarify. You want the letter/string that is at the same X/Y location as the numbers in Max? Commented Mar 10, 2017 at 22:54
  • Yes, I want the letter that corresponds with the max value only Commented Mar 10, 2017 at 22:57
  • No, you don't have four arrays, you have four lists. At least, that's what you have in your code. And your code would throw an AttributeError Commented Mar 10, 2017 at 22:58

1 Answer 1

1

You can use argmax to get the index of the maximum values and advanced indexing to pick up the names from the names array:

import numpy as np

names = np.array([A, B, C, D])
values = np.array([a, b, c, d])

# use argmax to find out the index of the max values
index_max = values.argmax(0)
​
# use index to pick up corresponding names using advance indexing
names[index_max, np.arange(names.shape[1])]

# array(['foo', 'spam', 'bar'], 
#       dtype='<U4')
Sign up to request clarification or add additional context in comments.

3 Comments

What if A, B, C and D are not so straightforward and contain lots of different words that I won't be able to enter manually as "ABCD"? In my actual data its plant species names
Your example is a little vague to me. Do you mean you really have four lists where each list contains only one unique value?
Apologies, my error. Yes, I'll edit my question to reflect this

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.