0

I have a dataframe which has array of elements, i would like to get the index value of the maximum number in the array using python,

Desired output:

Elements    Max Value Index

[3,4,5]              2

[2,0,1]              0

I tried using:

df['Max Value Index'] = df["Elements"].apply(lambda x:max(x))

It gives me the maximum value, , I just need the index number, i tried many ways but not able to get that, can anybody please help?

2 Answers 2

1

Try idxmax(x)

df['Max Value Index'] = df["Elements"].apply(lambda x:idxmax(x))
Sign up to request clarification or add additional context in comments.

Comments

1

You could use numpy.argmax:

df['Max Value Index'] = df['Elements'].apply(lambda l:np.argmax(l))

Or in straight python:

df['Max Value Index'] = df["Elements"].apply(lambda l:l.index(max(l)))

Output:

    Elements  Max Value Index
0  [3, 4, 5]                2
1  [2, 0, 1]                0

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.