Row Wise Minimum of pandas dataframe

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

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

 

Create Dataframe:

 


## Create 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-minimum-of-pandas-dataframe-1

 

 

Row wise Minimum of all numeric columns in pandas

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

 

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


df['Minimum'] = df.min(axis=1, numeric_only=True)
df

resultant dataframe is

Row-Wise-minimum-of-pandas-dataframe-2

 

 

Row wise Minimum of specific columns in pandas

Row wise Minimum of specific columns in pandas is performed using min() 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 Minimum and will be stored under new column as shown below.

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

df['non_maths_Minimum']=df.iloc[:,2:4].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-3

 

 

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

Row wise Minimum of columns in pandas is performed using min() 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 Minimum 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 Minimum by column index slicing in pandas : Method 1
##If the columns are in a sequential position, you can use a slice object.

df['Minimum']=df.iloc[:,1:4].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-2

 

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

Row wise Minimum of columns in pandas is performed using min() 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 Minimum 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 Minimum 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['Minimum']=df.values[:,1:4].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-2

 

Row wise Minimum 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 Minimum is performed with min() function and axis =1 and  will be stored under new column named “Minimum” as shown below

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


df['Minimum']=df.iloc[:,[1,2,3]].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-2

 

Row wise Minimum 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 minimum is performed with min() function and axis =1 by will be stored under new column  as shown below

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


df['Minimum']=df.values[:,[1,2,3]].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-2

 

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

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

 

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

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

 

OR

 

df['Minimum']=df[['Maths', 'Science', 'Geography']].min(axis = 1)
df

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

Row-Wise-minimum-of-pandas-dataframe-2

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    https://www.linkedin.com/in/sridhar-venkatachalam-74414252/ karaimadai@sg2plcpnl0204.prod.sin2.secureserver.net Venkatachalam Sridhar