2

I'm pulling prices ($ USD) of stuff from APIs into a dataframe, and I want to determine which of the items is worth the most. I know how to do this in R, but can't figure it out in python.

Here is an example of what I am trying to do:

import pandas as pd

d= {'name': ["item1", "item2", "item3"], 'price':[3,4,7]}
df=pd.DataFrame(data=d)

now I want something that will return which item name has the most value, which would look something like this in R:

print( df$price[ which( max(df$price) ) , 1] )

this code would return the name item3 since it is $7, "which" would determine the location of the max value in the dataframe (row 3), then it would return the name from the first column. "Which" does not work the same in python as R, so I have no idea how to do this and I can't figure it out.

Any ideas as to how to do this? I need the item name to feed into the next block of the code.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
DeFr0st
  • 59
  • 1
  • 1
  • 4
  • 1
    `df['price'].idxmax()` gives you the index label, `df.loc[df['price'].idxmax()]` gives you the corresponding row and `df.loc[df['price'].idxmax(), 'name']` gives you the corresponding name. – ayhan Nov 26 '17 at 14:31
  • perfect, that is exactly what i needed. – DeFr0st Nov 26 '17 at 14:56

0 Answers0