Articles

max function in pandas dataframe | max() pandas example

max function in pandas dataframe | max() pandas example


In this chapter of Pandas tutorial, we will discuss about 

  • max function in pandas dataframe,
  • example of max function in pandas,

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Create DataFrame

In this example, we will create dataframe with 4 rows and 4 columns with college data and assign index labels using index parameter.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



#display the dataframe

print(data)

Output:

create dataframe

Lets now use max function in pandas dataframe.

 

max function in pandas

max() function in pandas is used to return the maximum value from the pandas dataframe.

Syntax:

dataframe.max(axis)

Where,

1.      dataframe is the input dataframe

2.      axis is used to represent the row/column where maximum value is returned.

axis= 0  specifies row and axis=1 specifies column.

 

Note – axis is optional parameter – max() default take axis=0.


Example of max function in pandas

Example 1

In this example, we are applying max function in pandas dataframe without any parameters. So the result will be row-wise.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

#get the maximum value from the dataframe

print(data.max())

Output:

college_id          c-021
college_name         vvit
college_address    guntur
Total Staff          5644
dtype: object

From the above code, the max() function returned maximum value in each row.


Example 2:

In this example, we are applying max function in pandas dataframe to get maximum from rows by specifying axis=0 (which is similar to above example).

 

So the result will be row-wise.

import pandas as pd


#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



#get the maximum value from rows in the dataframe

print(data.max(axis=0))

Output:

college_id          c-021
college_name         vvit
college_address    guntur
Total Staff          5644
dtype: object

From the above code, the max() function returned maximum value from each row.


Example 3:

In this example, we are applying max() function to get maximum from columns by specifying axis=1. So the result will be column wise.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



#get the maximum value from columns in the dataframe

print(data.max(axis=1))

Output:

college1    1200
college2    3422
college3    5644
college4     670
dtype: int64

From the above code, the max() function returned maximum value from each column.


Example 4:

We can also get the maximum value from the particular columns in the dataframe using max function in pandas. We have to specify the column name with the dataframe.

Syntax:

dataframe[‘column’].max()
import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

#get the max value from particular columns

print(data['Total Staff'].max())

print(data['college_name'].max())

print(data['college_id'].max())

print(data['college_address'].max())

 

Output:

5644
vvit
c-021
Guntur

From the above code, we are going to return maximum value in each column with max() function separately.


Conclusion

In this article, we discussed how to select maximum value from the Pandas DataFrame using max() function. Finally we conclude that we will also get the maximum value from particular from the dataframe by specifying the column name.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 10,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!