Pandas DataFrame max()

Pandas

We can get maximum number in rows or columns by using max().

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data.max())
Output
NAME       Ron
ID           6
MATH        80
ENGLISH     80
What is the highest mark in MATH ?
print(my_data['MATH'].max()) # 80 
We can get the row or details of the record who got maximum mark in MATH
print(my_data[my_data['MATH'].max()==my_data['MATH']])
Output is here
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       80

Using axis

Axis of Two dimensional array We will use option axis=0 ( default ) by adding to above code.

( The last line is only changed )
print(my_data.max(axis=1))
Output is here
0    80
1    70
2    70
3    70
4    70
5    30

level option

For MultiIndex (hierarchical) axis we can specify the level.
import pandas as pd 
my_dict=pd.MultiIndex.from_arrays(
         [[1,2,3,4,5,6],
         [80,40,70,70,70,30],
         [80,70,40,50,60,30]],
names=['id','math','eng'])
my_data = pd.Series([4, 2, 0, 8,3,4], name='marks', index=my_dict)
print(my_data.max(level='math'))
Output
math
80    4
40    2
70    8
30    4

Handling NA data using skipna option

We will use skipna=True to ignore the null or NA data. Let us check what happens if it is set to True ( skipna=True )
import numpy as np
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,np.nan,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data.max(skipna=True))
Output
NAME       Ron
ID           6
MATH        80
ENGLISH     80

numeric_only

Default value is None, we can set it to True ( numeric_only=True ) to include only float, int, boolean columns. We can included all by setting it to False ( numeric_only=False ) . Let us see the outputs .
print(my_data.max(numeric_only=False))
Output is here
NAME       Ron
ID           6
MATH        80
ENGLISH     80
Pandas Plotting graphs min sum len std Filtering of Data


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer