In this article we will discuss how to find maximum value in rows & columns of a Dataframe and also it’s index position.

DataFrame.max()

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

Important Arguments:

  • axis : Axis along which maximumn 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 maximum value along the given axis i.e. either in rows or columns.

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

Suppose we have a Dataframe i.e.

Contents of the dataframe object dfObj are,

Get maximum values in every row & column of the Dataframe

Get maximum values of every column

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

Output:

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

Get maximum values of every row

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

Output:

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

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

Get maximum values of every column without skipping NaN

output:

As we have passed the skipna=False in max() 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 maximum value of that column.

Get maximum values of a single column or selected columns

To get the maximum value of a single column call the max() 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 maximum value from that only i.e.

Output:

Get row index label or position of maximum values of every column

DataFrame.idxmax()

We got the maximum value of each column or row, but what if we want to know the exact index position in every column or row where this maximum value exists ? To get the index of maximum 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 maximum value along rows and columns.
Let’s see how to use that

Get row index label of Maximum value in every column

Output:

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

Get Column names of Maximum value in every row

Output:

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

Complete example is as follows,

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.