Home » excel » python – How to find the maximum value of a column with pandas?

python – How to find the maximum value of a column with pandas?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a table with 40 columns and 1500 rows. I want to find the maximum value among the 30-32nd (3 columns). How can it be done? I want to return the maximum value among these 3 columns and the index of dataframe.

print(Max_kVA_df.iloc[30:33].max())

enter image description here

How to&Answers:

hi you can refer this example

import pandas as pd

df=pd.DataFrame({'col1':[1,2,3,4,5],
  'col2':[4,5,6,7,8],
  'col3':[2,3,4,5,7]
})

print(df)

#print(df.iloc[:,0:3].max())# Mention range of the columns which you want, In your case change 0:3 to 30:33, here  33 will be excluded
ser=df.iloc[:,0:3].max()
print(ser.max())

Output

8

Answer:

Select values by positions and use np.max:

Sample: for maximum by first 5 rows:

np.random.seed(123)
df = pd.DataFrame(np.random.randint(10, size=(10, 3)), columns=list('ABC'))
print (df)
   A  B  C
0  2  2  6
1  1  3  9
2  6  1  0
3  1  9  0
4  0  9  3

print (df.iloc[0:5])
   A  B  C
0  2  2  6
1  1  3  9
2  6  1  0
3  1  9  0
4  0  9  3

print (np.max(df.iloc[0:5].max()))
9

Answer:

Or use iloc this way:

print(df.iloc[[30, 31], 2].max())