In this post, we are going to learn how to Pandas sum all columns except one with examples Pandas is an analytical data library that stores data in tabular form, and the table in Pandas is called a dataframe that contains rows and columns. To use a Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.
How to Pandas sum columns to a new column
- sum(): return the sum of the given value over the requested axis.
- iloc(): Find the sum of columns based on indices or position
- loc(): FInd sum of columns by column name or label
1. How to Pandas sum all columns except one using drop()
The Python Pandas sum() function returns the sum of a given value over the requested axis. In this example, we are finding the sum of all columns of Pandas dataframe into a new column name ‘Total’ except column ‘Marks’ with the help of the drop() function it will drop column ‘marks’ and sum all columns.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Total'] = dfobj.drop('Marks', axis=1).sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306
2. How to Pandas sum all columns except one using iloc[]
In this example, we have skipped the sum of column ‘Marks’ by using the iloc[] property. We have selected the columns starting from index 2 to the end pass Because the index of the ‘Marks’ column is 2 and apply the sum() function along with axis =1 to get the sum of all columns except ‘Marks’.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Total'] = dfobj.iloc[:, 2:].sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306
3. How to Pandas sum all columns except one using loc[]
The pandas.DataFrame.loc[] property will select all columns exclude one column. The code df.loc[:,df.columns]
selects all columns or df.loc[:,df.columns != ‘Marks’] ignores ‘Marks’ column and the sum() function will sum all columns of Pandas dataframe except ‘Marks’ into new column Total_Fee.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Total'] = dfobj.loc[:, dfobj.columns != "Marks"].sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306
4. Panda sum all columns except one using series.difference()
The series. difference() method contains a list of columns that we want to exclude while selecting the data from the dataframe.In this below example, We have passed series. difference([‘Marks’]) that will exclude the ‘Marks’ column of the given dataframe and the sum() function will return the sum of all columns except ‘Marks’
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Total'] = dfobj[dfobj.columns.difference(["Marks"])].sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306
5. Pandas Sum all columns except one using loc() and isin()
We have used loc[] property along with the isin() method to find the sum of all columns except ‘Marks’.The isin() function is used with not operator to exclude column ‘Mark’ and select other columns.
import pandas as pd
data = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[97,97,100,100],
'Admit_fee':[201,203,205,206],
'Fee':[100,200,300,400],
'Tution_Fee':[400,500,600,700]
}
dfobj = pd.DataFrame(data)
dfobj['Total'] = dfobj.loc[:, ~dfobj.columns.isin(['Marks'])].sum(axis=1)
print(dfobj)
Output
Name Marks Admit_fee Fee Tution_Fee Total_Fee
0 Jack 97 201 100 400 701
1 Rack 97 203 200 500 903
2 Max 100 205 300 600 1105
3 David 100 206 400 700 1306