Reputation: 3357
I want to take max value of a pandas dataframe column and find the corresponding value in another column? Let's assume there are not duplicates in the maximum value, so you are always returning only one value. For example,
GPA ID
2.3 Tina1
3.4 Bob1
3.6 Lia1
2.9 Tina2
4.0 Blake1
4.5 Conor2
Here the max GPA is 4.5, but I want to return the corresponding id for the max GPA, so I would return Conor2. I am unsure of how to do this, so help would be greatly appreciated :) Thanks!
Upvotes: 5
Views: 5140
Reputation: 153570
Let's use idxmax
:
df.set_index('ID').idxmax()
Output:
GPA Conor2
dtype: object
A series with a single value Conor2
.
Upvotes: 3
Reputation: 38425
You can use
df.loc[df.GPA == df.GPA.max(), 'ID']
You get
5 Conor2
If you don't want the index but just the value, try
df.loc[df.GPA == df.GPA.max(), 'ID'].values[0]
You get
'Conor2'
Upvotes: 5