Home > other >  How to find index of an array which has maximum value in dataframe using python?
How to find index of an array which has maximum value in dataframe using python?

Time:06-23

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?

CodePudding user response:

Try idxmax(x)

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

CodePudding user response:

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
  • Related