I have a pandas data frame (indexed automatically 0 to n-1) and it contains several columns, among which are "price 1", "price 2", "price 3", and so on. I'm interested in finding the value of column "name" for the row which has the hight price (the max price picked from ALL "price x" columns). I knowI can select a view of my table that only contains prices and then do price_table_view.values.max() to get the highest price. But i want the index of this number so i can do a reverse look up on other columns. I've tried price_table_view.idxmax() to get a list of the indices of the highest value for each of the prices. But I just want the index of the highest price of these. How do I go about isolating the index of the max of the maxes? THANKS!
Asked
Active
Viewed 780 times
1 Answers
0
Try this:
In [124]: df
Out[124]:
ID price1 price2 price3
0 1 11 2 88
1 2 4 5 66
2 3 7 88 9
3 4 99 0 99
In [125]: subset = df.filter(regex='^price')
In [126]: subset
Out[126]:
price1 price2 price3
0 11 2 88
1 4 5 66
2 7 88 9
3 99 0 99
In [127]: np.unravel_index(subset.values.argmax(), subset.values.shape)
Out[127]: (3, 0)

MaxU - stand with Ukraine
- 205,989
- 36
- 386
- 419
-
Thanks! also found this other thread which takes a similar approach: http://stackoverflow.com/questions/29972894/pandas-dataframe-get-index-of-max-element – mik Jan 29 '17 at 14:35
-
However, this code seems to get the indices and then takes the minimum of the the index values, not the index of the minimum value. Does that make sense? – mik Jan 29 '17 at 14:59