{"id":5943,"date":"2023-10-29T15:41:43","date_gmt":"2023-10-29T10:11:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=5943"},"modified":"2023-11-10T12:06:51","modified_gmt":"2023-11-10T06:36:51","slug":"pandas-find-maximum-values-position-in-columns-or-rows-of-a-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-find-maximum-values-position-in-columns-or-rows-of-a-dataframe\/","title":{"rendered":"Pandas: Find maximum values & position in columns or rows of a Dataframe | How to find the max value of a pandas DataFrame column in Python?"},"content":{"rendered":"
In this article, we will discuss how to find maximum value & position in rows or columns of a Dataframe and its index position.<\/p>\n
Python pandas provide a member function in the dataframe to find the maximum value.<\/p>\n
Dataframe.max() accepts these arguments:<\/p>\n axis: Where max element will be searched<\/p>\n skipna: Default is True means if not provided it will be skipped.<\/p>\n Let’s create a dataframe,<\/p>\n Output:<\/strong><\/p>\n Here, you will find two ways to get the maximum values in dataframe<\/p>\n Also Check:\u00a0<\/span><\/p>\n In this, we will call the max() function to find the maximum value of every column in DataFrame.<\/p>\n Output:<\/strong><\/p>\n In this also we will call the max() function to find the maximum value of every row in DataFrame.<\/p>\n Output:<\/strong><\/p>\n So in the above example, you can see that it returned a series with a row index label and maximum value of each row.<\/p>\n Output:<\/strong><\/p>\n So in the above example, you can see that we have passed the ‘skipna=False’ in the max() function, So it included the NaN while searching for NaN.<\/p>\n If there is any NaN in the column then it will be considered as the maximum value of that column.<\/p>\n So for getting a single column maximum value we have to select that column and apply the max() function in it,<\/p>\n Here you can see that we have passed y\u00a0 Output:<\/strong><\/p>\n We can also pass the list of column names instead of passing single column like.,<\/p>\n Output:<\/strong><\/p>\n So in the above examples, you have seen how to get the max value of rows and columns but what if we want to know the index position of that row and column whereas the value is maximum, by using dataframe.idxmax() we get the index position.<\/p>\n Syntax-<\/p>\n Output:<\/strong><\/p>\n So here you have seen it showed the index position of the column where max value exists.<\/p>\n Output:<\/strong><\/p>\n So here you have seen it showed the index position of a row where max value exists.<\/p>\n So in this article, we have seen how to find maximum value & position in rows or columns of a Dataframe and its index position. Thank you!<\/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 Find Elements in a Dataframe<\/strong><\/p>\n In this article, we will discuss how to find maximum value & position in rows or columns of a Dataframe and its index position. DataFrame.max() Syntax Get maximum values in every row & column of the Dataframe Get maximum values of every column Get maximum values of every row Get maximum values of every column …<\/p>\nDataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)<\/code><\/p>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\nprint(dfObj)<\/pre>\n
x\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 y\u00a0 \u00a0 \u00a0 z\r\na 17\u00a0 \u00a0 \u00a015.0\u00a0 \u00a012.0\r\nb 53\u00a0 \u00a0 \u00a0NaN\u00a0 \u00a010.0\r\nc 46\u00a0 \u00a0 \u00a0 34.0\u00a0 \u00a011.0\r\nd 35\u00a0 \u00a0 \u00a0 45.0\u00a0 \u00a0NaN\r\ne 76\u00a0 \u00a0 \u00a0 26.0\u00a0 \u00a013.0\r\n<\/pre>\n
<\/a>Get maximum values in every row & column of the Dataframe<\/h2>\n
\n
<\/a>Get maximum values of every column<\/h3>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# Get a series containing maximum value of each column\r\nmaxValuesObj = dfObj.max()\r\nprint('Maximum value in each column : ')\r\nprint(maxValuesObj)<\/pre>\n
Maximum value in each column :\r\nx 76.0\r\ny 45.0\r\nz 13.0\r\n<\/pre>\n
<\/a>Get maximum values of every row<\/h3>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# Get a series containing maximum value of each row\r\nmaxValuesObj = dfObj.max(axis=1)\r\nprint('Maximum value in each row : ')\r\nprint(maxValuesObj)<\/pre>\n
Maximum value in each row :\r\na\u00a0 \u00a017.0\r\nb\u00a0 \u00a053.0\r\nc\u00a0 \u00a046.0\r\nd\u00a0 \u00a045.0\r\ne\u00a0 \u00a076.0\r\n\r\n<\/pre>\n
<\/a>Get maximum values of every column without skipping NaN<\/h2>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# Get a series containing maximum value of each column without skipping NaN\r\nmaxValuesObj = dfObj.max(skipna=False)\r\nprint('Maximum value in each column including NaN: ')\r\nprint(maxValuesObj)<\/pre>\n
Maximum value in each column including NaN:\r\nx 76.0\r\ny NaN\r\nz NaN\r\n<\/pre>\n
<\/a>Get maximum values of a single column or selected columns<\/h2>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# Get maximum value of a single column 'y'\r\nmaxValue = dfObj['y'].max()\r\nprint(\"Maximum value in column 'y': \" , maxValue)<\/pre>\n
maxValue = dfObj['y'].max()<\/code>for getting max value in that column.<\/p>\n
Maximum value in column 'y': 45.0\r\n<\/pre>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# Get maximum value of a single column 'y'\r\nmaxValue = dfObj[['y', 'z']].max()\r\nprint(\"Maximum value in column 'y' & 'z': \")\r\nprint(maxValue)<\/pre>\n
Maximum value in column 'y' & 'z':\r\ny 45.0\r\nz 13.0\r\n<\/pre>\n
<\/a>Get row index label or position of maximum values of every column<\/h2>\n
DataFrame.idxmax()<\/h3>\n
DataFrame.idxmax(axis=0, skipna=True)<\/code><\/p>\n
<\/a>Get row index label of Maximum value in every column<\/h2>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# get the index position of max values in every column\r\nmaxValueIndexObj = dfObj.idxmax()\r\nprint(\"Max values of columns are at row index position :\")\r\nprint(maxValueIndexObj)\r\n<\/pre>\n
Max values of columns are at row index position :\r\nx e\r\ny d\r\nz e\r\ndtype: object\r\n<\/pre>\n
<\/a>Get Column names of Maximum value in every row<\/h2>\n
import pandas as pd\r\nimport numpy as np\r\n# List of Tuples\r\nmatrix = [(17, 15, 12),\r\n (53, np.NaN, 10),\r\n (46, 34, 11),\r\n (35, 45, np.NaN),\r\n (76, 26, 13)\r\n ]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))\r\n# get the column name of max values in every row\r\nmaxValueIndexObj = dfObj.idxmax(axis=1)\r\nprint(\"Max values of row are at following columns :\")\r\nprint(maxValueIndexObj)<\/pre>\n
Max values of row are at following columns :\r\na x\r\nb x\r\nc x\r\nd y\r\ne x\r\ndtype: object\r\n<\/pre>\n
Conclusion:<\/h3>\n
\n