0

I am looking for a maximum value within my pandas dataframe but only within certain index range:

df.loc[df['Score'] == df['Score'].iloc[430:440].max()]

This gives me a pandas.core.frame.DataFrame type output with multiple rows. I specifically need the the index integer of the maximum value within iloc[430:440] and only the first index the maximum value occurs.

Is there anyway to limit the range of the .loc method?

Thank you

Shion
  • 319
  • 4
  • 12
  • 1
    Please include a _small_ subset of your data as a __copyable__ piece of code that can be used for testing as well as your expected output for the __provided__ data. See [MRE - Minimal, Reproducible, Example](https://stackoverflow.com/help/minimal-reproducible-example), and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker May 26 '21 at 22:09

1 Answers1

0

If you just want the index:

i = df['Score'].iloc[430:440].idxmax()

If you want to get the row as well:

df.loc[i]

If you want to get the first row in the entire dataframe with that value (rather than just within the range you specified originally):

df[df['Score'] == df['Score'].iloc[430:440].max()].iloc[0]
rudolfovic
  • 3,163
  • 2
  • 14
  • 38