I am looking for a maximum value within my pandas dataframe but only within certain index range:
JavaScript
x
2
1
df.loc[df['Score'] == df['Score'].iloc[430:440].max()]
2
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:
JavaScript
1
2
1
i = df['Score'].iloc[430:440].idxmax()
2
If you want to get the row as well:
JavaScript
1
2
1
df.loc[i]
2
If you want to get the first row in the entire dataframe with that value (rather than just within the range you specified originally):
JavaScript
1
2
1
df[df['Score'] == df['Score'].iloc[430:440].max()].iloc[0]
2