Articles

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

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


In this pandas tutorial, we will discuss about:

  • aggregate min function in pandas,
  • pandas find min value in row,
  • find min value in pandas column

Aggregate min() function is applied on the pandas DataFrame to find min in 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 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 is created below

       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

Lets use above dataframe to understand aggregate min function in pandas.


aggregate min function in pandas

If we want to get the minimum value of the columns in the dataframe or find min in pandas dataframe, then we have to use aggregate functions.

 

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

 

For aggregation, the method used is agg() and for minimum, the method used is min.

There are three scenarios of using this function to find min in pandas dataframe. Let's discuss one by one.


Scenario 1 : min aggregation over the rows pandas

In this scenario to find min in pandas dataframe, we will get the min over each row in the dataframe.

Syntax:

dataframe.agg('min')

where, dataframe is the input dataframe.

 

Examplepandas find min value in row

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'])

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

Output: From the above pandas find min value in row example, we will return min aggregation over the rows

length      4.5
breadth     4.5
area       20.0
dtype: float64

Lets see second scenario on find min in pandas dataframe.


Scenario 2 : min aggregation per column

In this scenario to find min in pandas dataframe, we will get the minimum for the mentioned columns in the dataframe.

Syntax:

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

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

 

Examplefind min value in pandas column

In this find min value in pandas column example, we will get minimum 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'])

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

OutputFrom the above find min value in pandas column example, we will return the min aggregation per column

length     4.5
area      20.0
dtype: float64

Lets see another approach to find min value in pandas column.


Scenario - 3 : min aggregation over the columns

In this find min value in pandas column scenario, we will get the minimum value over each column in the dataframe.

Syntax:

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

where, dataframe is the input dataframe.

 

Examplefind min value in pandas column

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("min", axis="columns")

OutputFrom the above example, we will return the aggregation over the columns

one      5.6
two      4.5
three    4.5
four     5.3
dtype: float64

Thus we have seen how to find the minimum value in a column using pandas.

 

This concludes our session on pandas find min value in row & find min value in pandas column.


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!