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.

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.

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

Suppose we have a Dataframe i.e.

Contents of the dataframe object dfObj are,

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.

Output:

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.

Output:

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

output:

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.

Output:

There is an another way too i.e.

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.

Output:

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.

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

Output:

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

Output:

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,

Output:

 

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,