Srikanth Varma
Srikanth Varma

Reputation: 175

How to find the max value between two indices in a pandas dataframe

If I have a dataframe like:

  | number | dummy
------------------
0 | 1      | 45
1 | 5      | 435
2 | 10     | 112
3 | 7
4 | 8
5 | 9

How do I find the max value between index 2 and 4 under column 'number' which in this case is 10.

Upvotes: 3

Views: 1456

Answers (2)

Karn Kumar
Karn Kumar

Reputation: 8816

Even can be used:

>>> df.iloc[2:4,:].loc[:,'number'].max()
10

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

If using index labels between 2 and 4 use loc:

df.loc[2:4, 'number'].max()

Output:

10

If using index integer positions 2nd through the 4th labels, then use iloc:

df.iloc[2:5, df.columns.get_loc('number')].max()

Note: you must use get_loc to get the integer position of the column 'number'

Output:

10

Upvotes: 4

Related Questions