Row Wise Sum of pandas dataframe

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

  • Row wise sum of all numeric columns in pandas
  • Row wise sum of specific columns in pandas
  • Row wise sum of pandas dataframe by column index slicing
  • Row wise sum of pandas dataframe by column index using list
  • Row wise sum 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 Sum of pandas dataframe 1

 

Row wise sum of all numeric columns in pandas

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

 

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

df['total'] = df.sum(axis=1, numeric_only=True)
df

resultant dataframe is

Row Wise Sum of pandas dataframe 2

 

 

 

Row wise sum of specific columns in pandas

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

 

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

df['non_maths_total']=df.iloc[:,2:4].sum(axis = 1)
df

Science and Geography Score is summed up and new column “non_maths_total” is created so  resultant dataframe is

Row Wise Sum of pandas dataframe 3

 

 

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

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


df['total']=df.iloc[:,1:4].sum(axis = 1)
df

Columns which is sliced by specified index is summed up and new column “total” is created so resultant dataframe is

Row Wise Sum of pandas dataframe 2

 

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

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


df['total']=df.values[:,1:4].sum(axis = 1)
df

Columns which is sliced by specified index is summed up and new column “total” is created so resultant dataframe is

Row Wise Sum of pandas dataframe 2

 

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

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

df['total']=df.iloc[:,[1,2,3]].sum(axis = 1)
df

Columns which is indexed using is summed up and new column “total” is created so resultant dataframe is

Row Wise Sum of pandas dataframe 2

 

 

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

 

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

df['total']=df.values[:,[1,2,3]].sum(axis = 1)
df

index is passed as list and is summed up at row level and new column “total” is created so resultant dataframe is

Row Wise Sum of pandas dataframe 2

 

Row wise sum of pandas dataframe by column name (label): Method 1

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

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

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

 

OR

 

df['total']=df[['Maths', 'Science', 'Geography']].sum(axis = 1)
df

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

Row Wise Sum of pandas dataframe 2

 

Row wise sum of pandas by column name (label): Method 2

Turn each column into a pandas Series and sum them individually will yield row wise sum. An alternative is to turn each column into a pandas Series and sum them individually. In my opinion, this isn’t a very elegant solution. It’s faster than the list subset but not as fast as the slice subset.

 

df['total']=df['Maths'] + df['Science'] + df['Geography']
df

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

Row Wise Sum 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.