Articles

Aggregate max() function pandas | find max value in pandas row & column

Aggregate max() function pandas | find max value in pandas row & column


In this pandas tutorial, we will discuss about:

  • aggregate max pandas,
  • find max value in pandas row,
  • find max value in pandas column,
  • find max value in each column pandas

Aggregate max() function or aggregate max pandas applied on the pandas DataFrame.

 

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

 

Example: Create Dataframe

In this example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: dataframe created

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Now lets use this dataframe to understand aggregate max pandas.


aggregate max pandas

If we want to get the maximum value of the columns in the dataframe, then we have to use aggregate functions.

 

In that, max is one of the function, which will return maximum value (row/column) of the dataframe.

 

For aggregation, the method used is agg() and for maximum, the method used is max.

There are three scenarios of using this function to get aggregate max pandas. Let's discuss one by one.


Scenario 1 : max aggregation over the rows

In this aggregate max pandas scenario, we will get the max over each row in the dataframe or find max value in pandas row.

Syntax:

dataframe.agg('max')

where, dataframe is the input dataframe.

 

Examplefind max value in pandas row

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#max aggregation over the rows
data.agg('max')

Outputfind max value in pandas row result

In this code, we will get maximum value in each column.

length      7.8
breadth    21.5
area       56.0
dtype: float64

Now lets see how to find max value in pandas column.


Scenario 2 : max aggregation per column

In this aggregate max pandas scenario, we will get the maximum for the mentioned columns in the dataframe.

Syntax:

dataframe.agg({'column' : 'max', ..............})

where, dataframe is the input dataframe and column is the column name to get maximim.

 

Examplefind max value in pandas column

In this aggregate max pandas example, we will get maximim value in length and area columns.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#max aggregation per column
data.agg({'length' : 'max', 'area' : 'max'})

Outputfind max value in pandas column result

In this example, we will get maximum values in length and area columns

length     7.8
area      56.0
dtype: float64

Lets see one more example on find max value in pandas column.


Scenario 3 : max aggregation over the columns

In this  scenario, we will get the maximum value over each column in the dataframe.

Syntax:

dataframe.agg("max", axis="columns")

where, dataframe is the input dataframe.

 

Examplefind max value in each column pandas

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# aggregation over the columns
data.agg("max", axis="columns")

Outputfind max value in each column pandas result

one      20.0
two      56.0
three    43.0
four     45.0
dtype: float64

Thus we have seen how to find max value in pandas row, find max value in pandas column, find max value in each column pandas.


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 : May 06,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!