Find max value index in rows and columns of Dataframe

In this post, we are going to learn how to Find max value index in rows and columns of Dataframe with programs examples using the built dataframe class member function max().

Pandas Dataframe max() function


The Pandas dataframe max() function is used to find the maximum value in a given object. The given object can be a series or a Dataframe. If the given object is a dataframe that it will return the maximum value along with the axis. The default axis is the index axis.

Syntax

DataFrame.max(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Parameters

  • axis: It is 0 for row and 1 for column.
  • skipna: It is a boolean value and the default is TRUE which excludes NA/NULL values.
  • level: int or level name, default None :
    • If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
  • numeric_only: boolean, default None
    • Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
  • **kwargs : Additional keyword arguments to be passed to the function

1. Find maximum value in each row of dataframe


In this python program example, we are finding the max value in each row of the dataframe by using the max() function and passing an argument as axis =1.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)

print(df)

maxval = df.max(axis=1)

print('\n max value in each row:\n ',maxval)

Output

   Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 max value in each row:
  0    600.0
1    500.0
2    700.0
3    350.0
dtype: float64

Popular Post


2. Find maximum value in each column of dataframe


In this example, we are finding the max value in each column of the dataframe by using the max() function without an argument.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)

print(df)

maxval = df.max( )

print('\n max value in each column:\n ',maxval)

Output

   Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 max value in each column:
  Amount    700.0
Marks     200.0
fee       500.0
dtype: float64

3. Find the maximum value of column with NAN value


The default value of the max() function is skipna = True if we want to include the NAN while finding the max value. We can set the skipna = False as we have done in the below example.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)

print(df)

maxval = df.max(skipna=False )

print('\n max value in each column with NAN:\n ',maxval)

Output

   Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

  max value in each column with NAN:
  Amount      NaN
Marks     200.0
fee       500.0
dtype: float64

4. Find max value in single or mutiple selected column


Sometimes instead of finding max value in every column, we need to get max values from selected single or multiple columns by providing single or multiple columns as a list.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)


#single column max value

max_val  = df['Amount'].max()


print('\n max value of single column:\n ',max_val)




#mutiple columns max value
maxval_muti_column = df[['Amount','fee']].max()


                        
print('\n max value of mutiple  column:\n ',maxval_muti_column)

Output

 max value of single column:
  700.0

 max value of mutiple  column:
  Amount    700.0
fee       500.0
dtype: float64

5. Find index of max value in every column


To find the max value index in every column we have used the idxmax() function of the dataframe class.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)


#single column max value

max_val  = df.idxmax()


print('\n max value index in every column:\n ',max_val)

Output

 max value index of every column:
  Amount    2
Marks     3
fee       1
dtype: int64

6. Find column name max value in every row


By using idmax() function we can find the index of max value in every row of the dataframe as we are doing in the below example.

Program Example

import pandas as pd
import numpy as np
  
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
  
df = pd.DataFrame(Student_dict)


#column name of max value in every row

column_name  = df.idxmax(axis=1)


print('\n max value column name of every row :\n ',column_name)




Output

 max value column name of every row :
  0    Amount
1       fee
2    Amount
3       fee
dtype: object

Summary

In this post, we have learned how to Find max value index in rows and columns of Dataframe with the help of examples.