Skip to content
Advertisement

limit pandas .loc method output within a iloc range

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

Advertisement

Answer

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]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement