{"id":8547,"date":"2021-06-10T14:21:07","date_gmt":"2021-06-10T08:51:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=8547"},"modified":"2021-11-22T18:45:25","modified_gmt":"2021-11-22T13:15:25","slug":"pandas-dataframe-get-minimum-values-in-rows-or-columns-their-index-position","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-dataframe-get-minimum-values-in-rows-or-columns-their-index-position\/","title":{"rendered":"Pandas Dataframe: Get minimum values in rows or columns & their index position"},"content":{"rendered":"
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.<\/p>\n
A member function is provided by Python’s Pandas library i.e. Some Arguments:<\/p>\n Now, we will see the implementation of these one by one.<\/p>\n 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.<\/p>\n To find the minimum values in each row in DataFrame we have to call In above cases we saw that it has skipped NaN, if we want we can also include NaN.<\/p>\n To get minimum value of every column without skipping NaN we have to pass skipna=False.<\/em><\/p>\n We can get minimum value of single column by calling min() member function by selecting that single column from given dataframe.<\/p>\n We can also get minimum value of selected columns by passing list of those columns.<\/p>\n We can also get the position of minimum value of DataFrame using pandas library function i.e. idxmin().<\/em><\/p>\n We can create a series which contains column names as index and row index labels where minimum element is found.<\/p>\n We can also create a series which contains row index labels as index and column names as values where each row has minimum value.<\/p>\n Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n Read more Articles on Python Data Analysis Using Padas \u2013 Select items from a Dataframe<\/strong><\/p>\n 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() \u00a0which can find the minimum …<\/p>\nDataFrame.min()<\/code> \u00a0which can find the minimum value in a dataframe.<\/p>\n
Syntax:- DataFrame.min(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)<\/pre>\n
\n
\n
Get minimum values in every row & column of the Dataframe :<\/h3>\n
<\/a>Get minimum values of every column :<\/h3>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\nprint('Original Dataframe :')\r\nprint(datafObj)\r\n# Get a series that contains minimum values in each column of dataframe\r\nminValues = datafObj.min()\r\nprint('Minimum value in each column of dataframe are : ')\r\nprint(minValues)\r\n<\/pre>\n
Output :\r\nOriginal Dataframe :\r\na\u00a0\u00a0\u00a0\u00a0 b\u00a0\u00a0\u00a0\u00a0 c\r\n1\u00a0 10\u00a0 20.0\u00a0 15.0\r\n2\u00a0 35\u00a0\u00a0 NaN\u00a0 21.0\r\n3\u00a0 18\u00a0 58.0\u00a0 65.0\r\n4\u00a0 11\u00a0 52.0\u00a0\u00a0 NaN\r\n5\u00a0 98\u00a0 34.0\u00a0 99.0\r\nMinimum value in each column of dataframe are :\r\na\u00a0\u00a0\u00a0 10.0\r\nb\u00a0\u00a0\u00a0 20.0\r\nc\u00a0\u00a0\u00a0 15.0\r\ndtype: float64<\/pre>\n
<\/a>Get minimum values of every row :<\/h3>\n
min()<\/code> member function and pass argument axis=1 <\/em>with DataFrame object.<\/p>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\nprint('Original Dataframe :')\r\nprint(datafObj)\r\n# Get a series that contains minimum element in each rows of dataframe\r\nminValues = datafObj.min(axis=1)\r\nprint('Minimum value in each row of dataframe are : ')\r\nprint(minValues)\r\n<\/pre>\n
Output :\r\nOriginal Dataframe :\r\na\u00a0\u00a0\u00a0\u00a0 b\u00a0\u00a0\u00a0\u00a0 c\r\n1\u00a0 10\u00a0 20.0\u00a0 15.0\r\n2\u00a0 35\u00a0\u00a0 NaN\u00a0 21.0\r\n3\u00a0 18\u00a0 58.0\u00a0 65.0\r\n4\u00a0 11\u00a0 52.0\u00a0\u00a0 NaN\r\n5\u00a0 98\u00a0 34.0\u00a0 99.0\r\nMinimum value in each row of dataframe are :\r\n1\u00a0\u00a0\u00a0 10.0\r\n2\u00a0\u00a0\u00a0 21.0\r\n3\u00a0\u00a0\u00a0 18.0\r\n4\u00a0\u00a0\u00a0 11.0\r\n5\u00a0\u00a0\u00a0 34.0\r\ndtype: float64<\/pre>\n
<\/a>Get minimum values of every column without skipping NaN :<\/h3>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\n# Get a series that contains minimum elements in each column including NaN\r\nminValues = datafObj.min(skipna=False)\r\nprint('Minimum value in each column including NaN of dataframe are : ')\r\nprint(minValues)\r\n<\/pre>\n
Output :\r\nMinimum value in each column including NaN of dataframe are :\r\na\u00a0\u00a0\u00a0 10.0\r\nb\u00a0\u00a0\u00a0\u00a0 NaN\r\nc\u00a0\u00a0\u00a0\u00a0 NaN\r\ndtype: float64<\/pre>\n
<\/a>Get minimum values of a single column or selected columns :<\/h3>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\n# Get minimum element of a single column 'y'\r\nminValues = datafObj['c'].min()\r\nprint(\"minimum value in column 'c' is : \" , minValues)\r\n<\/pre>\n
Output :\r\nminimum value in column 'c' is :\u00a0 15.0<\/pre>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\n# Get minimum value of a 'a' & 'b' columns of dataframe\r\nminValues = datafObj[['a', 'b']].min()\r\nprint(\"minimum value in column 'a' & 'b' are : \")\r\nprint(minValues)\r\n<\/pre>\n
Output :\r\nminimum value in column 'a' & 'b' are :\r\na\u00a0\u00a0\u00a0 10.0\r\nb\u00a0\u00a0\u00a0 20.0\r\ndtype: float64<\/pre>\n
<\/a>Get row index label or position of minimum values of every column :<\/h3>\n
DataFrame.idxmin() :<\/h4>\n
Syntax<\/u>:- DataFrame.idxmin(axis=0, skipna=True)<\/pre>\n
Get row index label of minimum value in every column :<\/h3>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\n# Get the index position of minimum values in every column of dataframe\r\nminValuesIndex = datafObj.idxmin()\r\nprint(\"min values of columns are at row index position :\")\r\nprint(minValuesIndex)\r\n<\/pre>\n
Output :\r\nmin values of columns are at row index position :\r\na\u00a0\u00a0\u00a0 1\r\nb\u00a0\u00a0\u00a0 1\r\nc\u00a0\u00a0\u00a0 1\r\ndtype: object<\/pre>\n
<\/a>Get Column names of minimum value in every row<\/h3>\n
import pandas as sc\r\nimport numpy as dc\r\n# List of Tuples\r\nmatrix = [(10, 20, 15),\r\n (35, dc.NaN, 21),\r\n (18, 58, 65),\r\n (11, 52, dc.NaN),\r\n (98, 34, 99)\r\n ]\r\n# Creation of DataFrame object\r\ndatafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))\r\n# Get minimum value of elements in row at respective column\r\nminValuesIndex = datafObj.idxmin(axis=1)\r\nprint(\" Minimum value in row at respective column of dataframe :\")\r\nprint(minValuesIndex)\r\n<\/pre>\n
Output :\r\nMinimum value in row at respective column of dataframe :\r\n1\u00a0\u00a0\u00a0 a\r\n2\u00a0\u00a0\u00a0 c\r\n3\u00a0\u00a0\u00a0 a\r\n4\u00a0\u00a0\u00a0 a\r\n5\u00a0\u00a0\u00a0 b\r\ndtype: object<\/pre>\n
\n