Articles

min in pandas dataframe | find minimum in pandas dataframe

min in pandas dataframe | find minimum in pandas dataframe


In this pandas tutorial, we will discuss about:

  • min in pandas dataframe,
  • pandas find minimum value in row,
  • pandas find minimum value in column,

Here we will learn to find min in pandas dataframe. So lets understand and create a dataframe first that will be used in our examples.

 

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 pandas

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 pandas dataframe

min() function in pandas is used to get the return the minimum value from the pandas dataframe.

Syntax:

dataframe.min(axis)

Where,

1.      dataframe is the input dataframe

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

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

 

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

Example 1pandas find minimum value in row

In this example, we are applying min() function without any parameters to find minimum in pandas dataframe. 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 minimum value from the dataframe

print(data.min())

Output: Result to find minimum in pandas dataframe from above code is below

college_id                     c-001
college_name       Andhra University
college_address               guntur
Total Staff                      670
dtype: object

The above code will take default axis as 0. So it will return the minimum value in each row.


Example 2find minimum in pandas dataframe (axis=0)

In this example, we are applying min() function to find minimum in pandas dataframe from rows by specifying axis=0.

For axis=0, pandas find minimum value in row, so the result will be row-wise.

 

Lets see the code snippet to get min in pandas dataframe by row:

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 minimum value from rows in the dataframe

print(data.min(axis=0))

Output: find minimum in pandas dataframe result is given below

college_id                     c-001
college_name       Andhra University
college_address               guntur
Total Staff                      670
dtype: object

The above code will take take axis as 0. So it will return the minimum value in each row.


Example 3pandas find minimum value in column

In this example, we are applying min() function to find minimum value in column pandas 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 minimum value from columns in the dataframe

print(data.min(axis=1))

Outputfind minimum value in column pandas result

college1    1200
college2    3422
college3    5644
college4     670
dtype: int64

The above code will take take axis as 1. So it will return the minimum value in each column


Example 4find minimum value in column pandas

We can also get the minimum value from the particular columns in the dataframe. We have to specify the column name with the dataframe.

Syntax:

dataframe[‘column’].min()

Lets see the code snippet to get min in pandas dataframe by column

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 minimum value from particular columns

print(data['Total Staff'].min())
print(data['college_name'].min())
print(data['college_id'].min())
print(data['college_address'].min())

Outputfind minimum value in column pandas result

670
Andhra University
c-001
guntur

The above code will return minimum value in each column specified.

 

Thus we have seen multiple examples related to finding min in pandas dataframe.


Conclusion

In this article, we discussed how to find minimum in pandas dataframe using min() function. Finally we conclude that we will also get the minimum 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 12,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!