Row Wise Maximum of pandas dataframe

In This Section we will be focusing on different methods to perform row wise Maximum of the pandas dataframe. i.e., Row wise Maximum of all the numeric column in pandas python, also Row wise Maximum of specified columns in pandas python. There are multiple ways to do it, we have used functions like iloc(), values() along with max() function to achieve the same. let’s look at each of these cases in pandas with an example for each.

  • Row wise Maximum of all numeric columns in pandas
  • Row wise Maximum of specific columns in pandas
  • Row wise Maximum of pandas dataframe by column index slicing
  • Row wise Maximum of pandas dataframe by column index using list
  • Row wise Maximum of pandas dataframe by column name (label)

 

 

Create Dataframe:

 

#Create a DataFrame

import pandas as pd
import numpy as np

d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'],
'Maths':[76,73,83,93,89,94],
'Science':[85,41,55,75,81,97],
'Geography':[78,65,55,88,87,98]}

df = pd.DataFrame(d,columns=['Name','Maths','Science','Geography'])
df

resultant dataframe is

Row Wise maximum of pandas dataframe 1

 

 

Row wise Maximum of all numeric columns in pandas

Row wise Maximum of all numeric columns in pandas is performed using df.max() function with axis =1 by passing parameter numeric_only=True.  Which will take up all the numeric column and will do row wise Maximum and will be stored under new column named ‘Maximum’ as shown below.

 

####### row wise Maximum of all numeric columns in pandas


df['Maximum'] = df.max(axis=1, numeric_only=True)
df

resultant dataframe is

Row Wise maximum of pandas dataframe 2

 

 

Row wise Maximum of specific columns in pandas

Row wise Maximum of specific columns in pandas is performed using max() function with axis =1 by filtering only the specific column using iloc.  Which will take up the specific numeric column and will do row wise Maximum and will be stored under new column  as shown below.

 

####### row wise Maximum of specific numeric columns in pandas

df['non_maths_Maximum']=df.iloc[:,2:4].max(axis = 1)
df

Maximum of Science and Geography Score is calculated and stored in new column “non_maths_Maximum”. So resultant dataframe is

Row Wise maximum of pandas dataframe 3

 

 

Row wise Maximum of pandas dataframe by column index slicing : Method 1

Row wise Maximum of columns in pandas is performed using max() function with axis =1 by filtering only the specific column using iloc.  Which will take up the specific numeric column and will do row wise Maximum and will be stored under new column as shown below.  If the columns are in a sequential position, you can use a slice object.

 
####### row wise Maximum by column index slicing in pandas : Method 1

df['Maximum']=df.iloc[:,1:4].max(axis = 1)
df

Columns which is sliced by specified index and maximum value is calculated and stored in new column “Maximum” so resultant dataframe is

Row Wise maximum of pandas dataframe 2

 

Row wise Maximum of pandas dataframe by column index slicing : Method 2

Row wise Maximum of columns in pandas is performed using max() function with axis =1 by filtering only the specific column using df.values.  Which will take up the specific numeric column and will do row wise Maximum and will be stored under new column as shown below.  If the columns are in a sequential position, you can use a slice object.

 

####### row wise Maximum by column index slicing in pandas : Method 2
#If the columns are in a sequential position, you can use a slice object.
##You can also use the DataFrame as a NumPy array object, by using df.values.


df['Maximum']=df.values[:,1:4].max(axis = 1)
df

Columns which is sliced by specified index and maximum value is calculated and stored in new column “Maximum” so resultant dataframe is

Row Wise maximum of pandas dataframe 2

 

 

Row wise Maximum of pandas dataframe by column index using list: Method 1

If the columns are not in a sequential position, you can use a list. Keep in mind that this subsetting method is twice as slow as using a slice. Column index is passed as a list and then row wise Maximum is performed with max() function and axis =1 and  will be stored under new column named “Maximum” as shown below

 
####### row wise Maximum/Maximum by column index using list in pandas : Method 1

df['Maximum']=df.iloc[:,[1,2,3]].max(axis = 1)
df

Row level maximum is created and stored in the new column named “Maximum”, so resultant dataframe is

Row Wise maximum of pandas dataframe 2

 

 

Row wise Maximum of pandas dataframe by column index using list: Method 2

If the columns are not in a sequential position, you can use a list. Column index is passed as a list and then row wise maximum is performed with max() function and axis =1 by will be stored under new column  as shown below

 
####### row wise Maximum by column index using list in pandas : Method 2

df['Maximum']=df.values[:,[1,2,3]].max(axis = 1)
df

Row level maximum is created and stored in the new column named “Maximum”, so resultant dataframe is

Row Wise maximum of pandas dataframe 2

 

 

Row wise Maximum of pandas dataframe by column name (label):

Column names are filters using .loc and max() function with axis=1 will perform the row wise  Maximum operation

 
####### row wise Maximum by column name (label) slicing in pandas : Method 1


df['Maximum']=df.loc[:,['Maths', 'Science', 'Geography']].max(axis = 1)
df

 

OR

 

df['Maximum']=df[['Maths', 'Science', 'Geography']].max(axis = 1)
df

new column “Maximum” is created which has row wise Maximum, so resultant dataframe is

Row Wise maximum of pandas dataframe 2