Bala
Bala

Reputation: 147

How to set no. of rows limit for pandas dataframe Maximum function

I have 100 rows in column B but I want to find Maximum value for only 99 rows.

If I use the below code it returns maximum value from 100 rows instead of 99 rows:

print(df1['noc'].max(axis=0)) 

Upvotes: 7

Views: 46877

Answers (1)

jezrael
jezrael

Reputation: 862801

Use head or iloc for select first 99 values and then get max:

print(df1['noc'].head(99).max()) 

Or as commented IanS:

print (df1['noc'].iloc[:99].max())

Sample:

np.random.seed(15)
df1 = pd.DataFrame({'noc':np.random.randint(10, size=15)})
print (df1)
    noc
0     8
1     5
2     5
3     7
4     0
5     7
6     5
7     6
8     1
9     7
10    0
11    4
12    9
13    7
14    5

print(df1['noc'].head(5).max()) 
8

print (df1['noc'].iloc[:5].max())
8

print (df1['noc'].values[:5].max())
8

Upvotes: 10

Related Questions