Pandas Sum DataFrame Columns With Examples

To sum pandas DataFrame columns (given selected multiple columns) using either sum(), iloc[], eval() and loc[] functions. Among these pandas DataFrame.sum() function returns the sum of the values for the requested axis, In order to calculate the sum of columns use axis=1. In this article, I will explain how to sum pandas DataFrame rows for given columns with examples.

1. Quick Examples of Sum DataFrame Columns

If you are in a hurry, below are some quick examples of how to sum pandas DataFrame by given or all columns.


# Below are quick example
# Using DataFrame.sum() to Sum of each row
df2 = df.sum(axis=1)

# Sum the rows of DataFrame
df['Sum'] = df.sum(axis=1)

# Just a few columns to sum
df['Sum'] = df['mathantics'] + df['science'] + df['english']

# Remove english column
col_list= list(df)
col_list.remove('english')

# sum specific columns
col_list= list(df)
col_list.remove('english')
df['Sum'] = df[col_list].sum(axis=1)

# Select 1 to 3 columns to sum
df['Sum']=df.iloc[:,1:3].sum(axis=1)

# Select 1 and 2 columns to sum Using DataFrame.iloc[] 
df['Sum']=df.iloc[:,[1,2]].sum(axis=1)

# Using DataFrame.iloc[] to select 2 and 3 columns to sum
df['Sum']=df.iloc[:,[2,3]].sum(axis=1)

# Sum columns Fee and Discount for row from r2 to r3
df['Sum'] = df.loc['r2':'r4',['mathantics','science']].sum(axis = 1)

# Using DataFrame.eval() function to sum of rows
df2 = df.eval('Sum = mathantics + english')

# Using DataFrame.loc[] and eval function to sum specific rows
df2 = df.loc['r2':'r4'].eval('Sum = mathantics + science')

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names studentname, mathantics, science and english.


import pandas as pd
studentdetails = {
       "studentname":["Ram","Sam","Scott","Ann","John"],
       "mathantics" :[80,90,85,70,95],
       "science" :[85,95,80,90,75],
       "english" :[90,85,80,70,95]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(studentdetails ,index=index_labels)
print(df)

Yields below output.


   studentname  mathantics  science  english
r1         Ram          80       85       90
r2         Sam          90       95       85
r3       Scott          85       80       80
r4         Ann          70       90       70
r5        John          95       75       95

2. Using DataFrame.sum() to Sum All Columns

Use DataFrame.sum() to get sum/total of a DataFrame for both rows and columns, to get the total sum of columns use axis=1 param. By default, this method takes axis=0 which means summing of rows.


# Using DataFrame.sum() to Sum of each row
df2 = df.sum(axis=1)
print(df2)

Yields below output. The above example calculates the sum of all numeric columns for each row. This also returns the index for each row to identify the result.


r1    255
r2    270
r3    245
r4    230
r5    265
dtype: int64

3. Sum the Columns of DataFrame Using DataFrame.sum()

If you notice the above output, the actual column values that are part of the sum are not returned by DataFrame.sum() function however, you can get all columns including the sum column by assigning the DataFrame.sum() to a DataFrame column. I would like to add a column 'Sum' which is the sum of column 'mathantics', 'science' and ‘english'.


# Sum the rows of DataFrame
df['Sum'] = df.sum(axis=1)
print(df)

# If you have few columns to sum
df['Sum'] = df['mathantics'] + df['science'] + df['english']
print(df)

Yields below output.


   studentname  mathantics  science  english  Sum
r1         Ram          80       85       90  255
r2         Sam          90       95       85  270
r3       Scott          85       80       80  245
r4         Ann          70       90       70  230
r5        John          95       75       95  265

4. Calculate Sum of Given Columns

To sum given or list of columns then create a list with all columns you wanted and slice the DataFrame with the selected list of columns and use the sum() function. Use df['Sum']=df[col_list].sum(axis=1) to get the total sum.


# Create List of columns
col_list= ['studentname', 'mathantics', 'science']
# sum specific columns
df['Sum'] = df[col_list].sum(axis=1)
print(df)

Yields below output.


   studentname  mathantics  science  english  Sum
r1         Ram          80       85       90  165
r2         Sam          90       95       85  185
r3       Scott          85       80       80  165
r4         Ann          70       90       70  160
r5        John          95       75       95  170

4. Using DataFrame.iloc[] to Get Sum of Columns

Use DataFrame.iloc[] to select which columns to sum and call sum(axis=1) on DataFrame.


# Select 1 to 3 columns to sum
df['Sum']=df.iloc[:,1:3].sum(axis=1)
print(df)

# Select 1 and 2 columns to sum Using DataFrame.iloc[] 
df['Sum']=df.iloc[:,[1,2]].sum(axis=1)
print(df)

# Using DataFrame.iloc[] to select 2 and 3 columns to sum
df['Sum']=df.iloc[:,[2,3]].sum(axis=1)
print(df)

Yields below output.


   studentname  mathantics  science  english  Sum
r1         Ram          80       85       90  165
r2         Sam          90       95       85  185
r3       Scott          85       80       80  165
r4         Ann          70       90       70  160
r5        John          95       75       95  170

5. Using DataFrame.loc[] to Sum All Rows From Row r2 to r4

By using DataFrame.loc[] function, select the columns by labels and then use sum(axis=1) function to calculate the total sum of columns. Using this you can also specify the rows you wanted to get the sum value. For rows that are not specified with loc[] results with NaN on the Sum column.


# Sum columns Fee and Discount for row from r2 to r3
df['Sum'] = df.loc['r2':'r4',['mathantics','science']].sum(axis = 1)
print(df)

Yields below output.


   studentname  mathantics  science  english    Sum
r1         Ram          80       85       90    NaN
r2         Sam          90       95       85  185.0
r3       Scott          85       80       80  165.0
r4         Ann          70       90       70  160.0
r5        John          95       75       95    NaN

To understand the differences between loc[] and iloc[], read the article pandas difference between loc[] vs iloc[]

6. Sum of Rows using DataFrame.eval() Function

Try DataFrame.eval('Sum=mathantics + english') to sum the rows using the eval function.


# Using DataFrame.eval() function to sum of rows
df2 = df.eval('Sum = mathantics + english')
print(df2)

Yields below output.


   studentname  mathantics  science  english  Sum
r1         Ram          80       85       90  170
r2         Sam          90       95       85  175
r3       Scott          85       80       80  165
r4         Ann          70       90       70  140
r5        John          95       75       95  190

7. Using DataFrame.loc[] and eval Function to Sum Specific Rows

You can use DataFrame.loc['r2':'r4'].eval('Sum = mathantics + science') to get the sum of columns for specific rows. To evaluate the sum of the rows with specified rows, use DataFrame.loc[] with the selected rows.


# Using DataFrame.loc[] and eval function to sum specific rows
df2 = df.loc['r2':'r4'].eval('Sum = mathantics + science')
print(df2)

Yields below output.


   studentname  mathantics  science  english  Sum
r2         Sam          90       95       85  185
r3       Scott          85       80       80  165
r4         Ann          70       90       70  160

8. Complete Example For Sum DataFrame Rows for Given Columns


import pandas as pd
studentdetails = {
       "studentname":["Ram","Sam","Scott","Ann","John"],
       "mathantics" :[80,90,85,70,95],
       "science" :[85,95,80,90,75],
       "english" :[90,85,80,70,95]
              }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(studentdetails ,index=index_labels)
print(df)

# Using DataFrame.sum() to Sum of each row
df2 = df.sum(axis=1)
print(df2)

# Sum the rows of DataFrame
df['Sum'] = df.sum(axis=1)
print(df)

# Just a few columns to sum
df['Sum'] = df['mathantics'] + df['science'] + df['english']
print(df)

# Remove english column
col_list= list(df)
col_list.remove('english')
print(col_list)

# sum specific columns
col_list= list(df)
col_list.remove('english')
df['Sum'] = df[col_list].sum(axis=1)
print(df)

# Select 1 to 3 columns to sum
df['Sum']=df.iloc[:,1:3].sum(axis=1)
print(df)

# Select 1 and 2 columns to sum Using DataFrame.iloc[] 
df['Sum']=df.iloc[:,[1,2]].sum(axis=1)
print(df)

# Using DataFrame.iloc[] to select 2 and 3 columns to sum
df['Sum']=df.iloc[:,[2,3]].sum(axis=1)
print(df)

# Sum columns Fee and Discount for row from r2 to r3
df['Sum'] = df.loc['r2':'r4',['mathantics','science']].sum(axis = 1)
print(df)

# Using DataFrame.eval() function to sum of rows
df2 = df.eval('Sum = mathantics + english')
print(df2)

# Using DataFrame.loc[] and eval function to sum specific rows
df2 = df.loc['r2':'r4'].eval('Sum = mathantics + science')
print(df2)

Conclusion

In this article, you have learned how to sum pandas DataFrame columns for all or given columns with examples.

Happy Learning !!

You May Also Like

References

Leave a Reply

You are currently viewing Pandas Sum DataFrame Columns With Examples