How to sum Pandas columns into new column

In this post, we will learn how to sum Pandas columns into new column or How to sum Pandas column and append result into new column. The 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 sum Pandas columns into 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 sum Pandas all columns into new column


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 single new column name ‘Total’.

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.sum(axis=1)

print(dfobj)

Output

    Name  Marks  Admit_fee  Fee  Tution_Fee  Total
0   Jack     97        201  100         400    798
1   Rack     97        203  200         500   1000
2    Max    100        205  300         600   1205
3  David    100        206  400         700   1406

3. How to Pandas sum specific columns into new column


Sometimes instead of all columns, there is a requirement to sum specific columns of Pandas dataframe. We pass the specific columns as a list cols = [‘Fee’, ‘Tution_Fee’] and pass the list to the sum() function along with axis =1 to get the sum of specific columns.

import pandas as pd
  
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy'],
    'Fee':[100,200,300,400],
    'Tution_Fee':[400,500,600,700]
}
  
dfobj = pd.DataFrame(data)


cols = ['Fee', 'Tution_Fee']

dfobj['Total_Fee'] = dfobj[cols].sum(axis=1)

print(dfobj)

Output

    Name  Marks Subject  Fee  Tution_Fee  Total_Fee
0   Jack     97    Math  100         400        500
1   Rack     97    Math  200         500        700
2    Max    100    Math  300         600        900
3  David    100     Phy  400         700       1100

3. Pandas sum multiple columns by condition


In this example, we are summing the column of the dataframe by using the loc[] property, Where we are checking if values of dfobj[‘Marks’] > 0 then sum [‘Fee’, ‘Tution_Fee’] columns add its result as a new column Total_Fee into dataframe.

  • Another way to sum the column of Pandas dataframe by condition using the where() method, is short and one-line code.
    • dfobj[‘Total_Fee’] = dfobj[[‘Fee’,’Tution_Fee’]].sum(axis=1).where(dfobj[‘Marks’] > 0, 0)
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_Fee'] = dfobj.loc[dfobj['Marks'] > 0,['Fee','Tution_Fee']].sum(axis=1)
dfobj['Total_Fee'].fillna(0, inplace=True)

#using where
dfobj['Total_Fee'] = dfobj[['Fee','Tution_Fee']].sum(axis=1).where(dfobj['Marks'] > 0, 0)



print(dfobj)


Output

   Name  Marks  Admit_fee  Fee  Tution_Fee  total
0   Jack     97        201  100         400    500
1   Rack     97        203  200         500    700
2    Max    100        205  300         600    900
3  David    100        206  400         700   1100

4. Pandas sum a column by column name


If we need to find the sum of single column of Pandas dataframe, we can achieve this by using the dfobj[‘Marks’].sum() function and display the result as we are doing in the below example.

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)

total_of_marks_col = dfobj['Marks'].sum()
print('Total of marks col:',total_of_marks_col)

Output

Total of marks col: 394

5. Pandas Sum multiple columns by name


To find the sum of multiple columns by column name, we have used the loc[] property in which we pass a list of columns name [‘Admit_fee’, ‘Fee’, ‘Tution_Fee’] whose sum needs to get into the 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_Fee'] = dfobj.loc[: , ['Admit_fee', 'Fee','Tution_Fee']].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

6. Pandas Sum multiple columns by position or index


In this example, we are summing multiple columns of pandas dataframe by their position or index. We have passed columns index as a list to the iloc[] property and used the sum function along axis=1 to get the sum of the column into a 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_Fee'] = dfobj.iloc[: , [2,3,4]].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

Summary

In this post, we have learned How to do the sum of columns in Pandas to a new column with example by using Pandas sum(),iloc(), and loc() function.