In this article we will discuss how to find minimum values in rows & columns of a Dataframe and also their index position.

### DataFrame.min()

Python’s Pandas Library provides a member function in Dataframe to find the minimum value along the axis i.e.

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

*Important Arguments:*

**axis :**Axis along which minimumn elements will be searched. For along index it’s 0 whereas along columns it’s 1**skipna :**(bool) If NaN or NULL to be skipped . Default is True i.e. if not provided it will be skipped.

It returns the minimum value along the given axis i.e. either in rows or columns.

**Advertisements**

Let’s use this to find the minimum value among rows and columns,

Suppose we have a Dataframe i.e.

# List of Tuples matrix = [(22, 16, 23), (33, np.NaN, 11), (44, 34, 11), (55, 35, np.NaN), (66, 36, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))

Contents of the dataframe object **dfObj** are,

x y z a 22 16.0 23.0 b 33 NaN 11.0 c 44 34.0 11.0 d 55 35.0 NaN e 66 36.0 13.0

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

#### Get minimum values of every column

To find minimum value of every column in DataFrame just call the min() member function with DataFrame object without any argument i.e.

# Get a series containing minimum value of each column minValuesObj = dfObj.min() print('minimum value in each column : ') print(minValuesObj)

Output:

minimum value in each column : x 22.0 y 16.0 z 11.0 dtype: float64

It returned a series with column names as index label and minimum value of each column in values.Â Similarly we can find minimum values in every row too,

#### Get minimum values of every row

To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument **axis=1** i.e.

# Get a series containing minimum value of each row minValuesObj = dfObj.min(axis=1) print('minimum value in each row : ') print(minValuesObj)

Output:

minimum value in each row : a 16.0 b 11.0 c 11.0 d 35.0 e 13.0 dtype: float64

It returned a series with row index label and minimum value of each row.

As we can see that it has skipped the **NaN** while finding the min value. We can include the **NaN** too if we want i.e.

## Get minimum values of every column without skipping NaN

# Get a series containing minimum value of each column without skipping NaN minValuesObj = dfObj.min(skipna=False) print('minimum value in each column including NaN: ') print(minValuesObj)

output:

minimum value in each column including NaN: x 22.0 y NaN z NaN dtype: float64

As we have passed the **skipna=False** in min() function, therefore it included the NaN to while searching for NaN.Â Also, if there is any NaN in the column then it will be considered as minimum value of that column.

## Get minimum values of a single column or selected columns

To get the minimum value of a single column call the min() function by selecting single column from dataframe i.e.

# Get minimum value of a single column 'y' minValue = dfObj['y'].min() print("minimum value in column 'y': " , minValue)

Output:

minimum value in column 'y': 16.0

There is an another way too i.e.

# Get minimum value of a single column 'y' minValue = dfObj.min()['y']

It will give the same result.

Instead of passing a single column name we can pass the list of column names too for selecting minimum value from that only i.e.

# Get minimum value of a single column 'y' minValue = dfObj[['y', 'z']].min() print("minimum value in column 'y' & 'z': ") print(minValue)

Output:

minimum value in column 'y' & 'z': y 16.0 z 11.0 dtype: float64

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

#### DataFrame.idxmin()

We got the minimum value of each column or row, but what if we want to know the exact index position in every column or row where this minimum value exists ?Â To get the index of minimum value of elements in row and columns, pandas library provides a function i.e.

DataFrame.idxmin(axis=0, skipna=True)

Based on the value provided in axis it will return the index position of minimum value along rows and columns.

Let’s see how to use that

#### Get row index label of minimum value in every column

# get the index position of min values in every column minValueIndexObj = dfObj.idxmin() print("min values of columns are at row index position :") print(minValueIndexObj)

Output:

min values of columns are at row index position : x a y a z b dtype: object

It’s a series containing the column names as index and row index labels where the minimum value exists in that column.

#### Get Column names of minimum value in every row

# get the column name of min values in every row minValueIndexObj = dfObj.idxmin(axis=1) print("min values of row are at following columns :") print(minValueIndexObj)

Output:

min values of row are at following columns : a y b z c z d y e z dtype: object

It’s a series containing the rows index labels as index and column names as values where the minimum value exists in that row.

**Complete example is as follows,**

import pandas as pd import numpy as np def main(): # List of Tuples matrix = [(22, 16, 23), (33, np.NaN, 11), (44, 34, 11), (55, 35, np.NaN), (66, 36, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) print('Original Dataframe Contents :') print(dfObj) print('***** Get minimum value in every column ***** ') # Get a series containing minimum value of each column minValuesObj = dfObj.min() print('minimum value in each column : ') print(minValuesObj) print('***** Get minimum value in every row ***** ') # Get a series containing minimum value of each row minValuesObj = dfObj.min(axis=1) print('minimum value in each row : ') print(minValuesObj) print('***** Get minimum value in every column without skipping NaN ***** ') # Get a series containing minimum value of each column without skipping NaN minValuesObj = dfObj.min(skipna=False) print('minimum value in each column including NaN: ') print(minValuesObj) print('***** Get minimum value in a single column ***** ') # Get minimum value of a single column 'y' minValue = dfObj['y'].min() print("minimum value in column 'y': " , minValue) # Get minimum value of a single column 'y' minValue = dfObj.min()['y'] print("minimum value in column 'y': " , minValue) print('***** Get minimum value in certain columns only ***** ') # Get minimum value of a single column 'y' minValue = dfObj[['y', 'z']].min() print("minimum value in column 'y' & 'z': ") print(minValue) print('***** Get row index label of minimum value in every column *****') # get the index position of min values in every column minValueIndexObj = dfObj.idxmin() print("min values of columns are at row index position :") print(minValueIndexObj) print('***** Get Column name of minimum value in every row *****') # get the column name of min values in every row minValueIndexObj = dfObj.idxmin(axis=1) print("min values of row are at following columns :") print(minValueIndexObj) if __name__ == '__main__': main()

**Output:**

Original Dataframe Contents : x y z a 22 16.0 23.0 b 33 NaN 11.0 c 44 34.0 11.0 d 55 35.0 NaN e 66 36.0 13.0 ***** Get minimum value in every column ***** minimum value in each column : x 22.0 y 16.0 z 11.0 dtype: float64 ***** Get minimum value in every row ***** minimum value in each row : a 16.0 b 11.0 c 11.0 d 35.0 e 13.0 dtype: float64 ***** Get minimum value in every column without skipping NaN ***** minimum value in each column including NaN: C:\Users\varun\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\_methods.py:32: RuntimeWarning: invalid value encountered in reduce x 22.0 return umr_minimum(a, axis, None, out, keepdims, initial) y NaN z NaN dtype: float64 ***** Get minimum value in a single column ***** minimum value in column 'y': 16.0 minimum value in column 'y': 16.0 ***** Get minimum value in certain columns only ***** minimum value in column 'y' & 'z': y 16.0 z 11.0 dtype: float64 ***** Get row index label of minimum value in every column ***** min values of columns are at row index position : x a y a z b dtype: object ***** Get Column name of minimum value in every row ***** min values of row are at following columns : a y b z c z d y e z dtype: object

**Advertisements**

Thanks for reading.