Pandas Dataframe: Get minimum values in rows or columns & their index position

Get minimum values in rows or columns & their index position

In this article we will learn to find minimum values in the rows & columns of a Dataframe and also get index position of minimum values.

DataFrame.min() :

A member function is provided by Python’s Pandas library i.e. DataFrame.min()  which can find the minimum value in a dataframe.

Syntax:- DataFrame.min(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Some Arguments:

  1. axis- It is the axis along which minimum elements is to be searched. It is index 0 for along the rows and index 1 for along the columns.
  2. skipna- (bool) It will skip NaN or Null. It’s default is True i.e. it will be skipped if not provided.

Now, we will see the implementation of these one by one.

Get minimum values in every row & column of the Dataframe :

Get minimum values of every column :

To find the minimum element in every column of dataframe we have to only call min() member function without any argument with the DataFrame object. It will return a series with column name as index label and minimum value of each column in values.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
print('Original Dataframe :')
print(datafObj)
# Get a series that contains minimum values in each column of dataframe
minValues = datafObj.min()
print('Minimum value in each column of dataframe are : ')
print(minValues)
Output :
Original Dataframe :
a     b     c
1  10  20.0  15.0
2  35   NaN  21.0
3  18  58.0  65.0
4  11  52.0   NaN
5  98  34.0  99.0
Minimum value in each column of dataframe are :
a    10.0
b    20.0
c    15.0
dtype: float64

Get minimum values of every row :

To find the minimum values in each row in DataFrame we have to call min() member function and pass argument axis=1 with DataFrame object.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
print('Original Dataframe :')
print(datafObj)
# Get a series that contains minimum element in each rows of dataframe
minValues = datafObj.min(axis=1)
print('Minimum value in each row of dataframe are : ')
print(minValues)
Output :
Original Dataframe :
a     b     c
1  10  20.0  15.0
2  35   NaN  21.0
3  18  58.0  65.0
4  11  52.0   NaN
5  98  34.0  99.0
Minimum value in each row of dataframe are :
1    10.0
2    21.0
3    18.0
4    11.0
5    34.0
dtype: float64

In above cases we saw that it has skipped NaN, if we want we can also include NaN.

Get minimum values of every column without skipping NaN :

To get minimum value of every column without skipping NaN we have to pass skipna=False.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get a series that contains minimum elements in each column including NaN
minValues = datafObj.min(skipna=False)
print('Minimum value in each column including NaN of dataframe are : ')
print(minValues)
Output :
Minimum value in each column including NaN of dataframe are :
a    10.0
b     NaN
c     NaN
dtype: float64

Get minimum values of a single column or selected columns :

We can get minimum value of single column by calling min() member function by selecting that single column from given dataframe.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum element of  a single column 'y'
minValues = datafObj['c'].min()
print("minimum value in column 'c' is : " , minValues)
Output :
minimum value in column 'c' is :  15.0

We can also get minimum value of selected columns by passing list of those columns.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum value of a 'a' & 'b' columns of dataframe
minValues = datafObj[['a', 'b']].min()
print("minimum value in column 'a' & 'b' are : ")
print(minValues)
Output :
minimum value in column 'a' & 'b' are :
a    10.0
b    20.0
dtype: float64

Get row index label or position of minimum values of every column :

DataFrame.idxmin() :

We can also get the position of minimum value of DataFrame using pandas library function i.e. idxmin().

Syntax:- DataFrame.idxmin(axis=0, skipna=True)

Get row index label of minimum value in every column :

We can create a series which contains column names as index and row index labels where minimum element is found.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get the index position of minimum values in every column of dataframe
minValuesIndex = datafObj.idxmin()
print("min values of columns are at row index position :")
print(minValuesIndex)
Output :
min values of columns are at row index position :
a    1
b    1
c    1
dtype: object

Get Column names of minimum value in every row

We can also create a series which contains row index labels as index and column names as values where each row has minimum value.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum value of elements in row at respective column
minValuesIndex = datafObj.idxmin(axis=1)
print(" Minimum value in row at respective column of dataframe :")
print(minValuesIndex)
Output :
Minimum value in row at respective column of dataframe :
1    a
2    c
3    a
4    a
5    b
dtype: object

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Select items from a Dataframe

Leave a Comment