Skip to content
Related Articles

Related Articles

Find maximum values & position in columns and rows of a Dataframe in Pandas
  • Last Updated : 14 May, 2021

In this article, we are going to discuss how to find maximum value and its index position in columns and rows of a Dataframe.

DataFrame.max()

Pandas dataframe.max() method finds the maximum of the values in the object and returns it. If the input is a series, the method will return a scalar which will be the maximum of the values in the series. If the input is a dataframe, then the method will return a series with maximum of values over the specified axis in the dataframe. The index axis is the default axis taken by this method.
 

Syntax : DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Parameters : 
axis : {index (0), columns (1)} 
skipna : Exclude NA/null values when computing the result 
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series 
numeric_only : Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
Returns : max : Series or DataFrame (if level specified) 
 

Let’s take an example of how to use this function. Let’s suppose we have a Dataframe 



Python3




import numpy as np
import pandas as pd
# List of Tuples
matrix = [(10, 56, 17),
          (np.NaN, 23, 11),
          (49, 36, 55),
          (75, np.NaN, 34),
          (89, 21, 44)
          ]
# Create a DataFrame
abc = pd.DataFrame(matrix, index = list('abcde'), columns = list('xyz'))
 
# output
abc

Output: 
 

 

How to find Maximum values of every column?

To find the maximum value of each column, call max() method on the Dataframe object without taking any argument. 

Python3




# find the maximum of each column
maxValues = abc.max()
 
print(maxValues)

Output : 
 



maximum values each column in dataframe

We can see that it returned a series of maximum values where the index is column name and values are the maxima from each column.

How to find maximum values of every row?

To find the maximum value of each row, call max() method on the Dataframe object with an argument axis = 1.  

Python3




# find the maximum values of each row
maxValues = abc.max(axis = 1)
print(maxValues)

Output : 
 

maximum values in dataframe-2

We can see that it returned a series of maximum values where the index is row name and values are the maxima from each row. We can see that in the above examples NaN values are skipped while finding the maximum values in any axis. We can include NaN values as well if we want.

How to find maximum values of every column without skipping NaN?

Python3




# find maximum value of each
# column without skipping NaN
maxValues = abc.max(skipna = False)
 
print(maxValues)

Output : 
 

Find maximum values & position in columns and rows of a Dataframe-3

By putting skipna=False we can include NaN values also. If any NaN value exists it will be considered as the maximum value.

How to find maximum values of a single column or selected columns?

To get the maximum value of a single column see the following example 

Python3




# find maximum value of a
# single column 'x'
maxClm = df['x'].max()
 
print("Maximum value in column 'x': " )
print(maxClm)

Output : 
 

maximum vale in column

We have another way to find maximum value of a column : 

Python3




# find maximum value of a
# single column 'x'
maxClm = df.max()['x']

The result will be same as above. 
Output: 
 

maximum vale in column

A list of columns can also be passed instead of a single column to find the maximum values of specified columns 

Python3




# find maximum values of a list of columns
maxValues = df[['x', 'z']].max()
 
print("Maximum value in column 'x' & 'z': ")
print(maxValues)

Output : 
 

 

How to get position of maximum values of every column?

DataFrame.idxmax(): Pandas dataframe.idxmax() method returns index of first occurrence of maximum over requested axis. While finding the index of the maximum value across any index, all NA/null values are excluded. 

Syntax: DataFrame.idxmax(axis=0, skipna=True)
Parameters : 
axis : 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise 
skipna : Exclude NA/null values. If an entire row/column is NA, the result will be NA
Returns : idxmax : Series 
 

Let’s take some examples to understand how to use it :

How to get row index label of Maximum value in every column  

Python3




# find the index position of maximum
# values in every column
maxValueIndex = df.idxmax()
 
print("Maximum values of columns are at row index position :")
print(maxValueIndex)

Output : 
 

It returns a series containing the column names as index and row as index labels where the maximum value exists in that column.

How to find Column names of Maximum value in every row?

Python3




# find the column name of maximum
# values in every row
maxValueIndex = df.idxmax(axis = 1)
 
print("Max values of row are at following columns :")
print(maxValueIndex)

Output :
 

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :