Table of Contents
Finding the sum of rows in a Pandas DataFrame can be done by using the DataFrame.sum() method. This method will return the sum of all the values in the DataFrame for each row. This can be useful for quickly summarizing data across a DataFrame. The result of the sum() method will be a Pandas Series with the index corresponding to the DataFrame’s columns. Additionally, the sum() method can be used with the axis argument to specify whether to sum across columns (axis=0) or across rows (axis=1). This allows for more fine-grained control over the summation of values.
Often you may be interested in calculating the sum of one or more rows in a pandas DataFrame. Fortunately you can do this easily in pandas using the function.
This tutorial shows several examples of how to use this function on the following DataFrame:
import pandas as pd import numpy as np #create DataFrame df = pd.DataFrame({'rating': [90, 85, 82, 88, 94, 90, 76, 75, 87, 86], 'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19], 'assists': [5, 7, 7, 8, 5, 7, 6, 9, 9, 5], 'rebounds': [8, np.nan, 10, 6, 6, 9, 6, 10, 10, 7]}) #view DataFrame df rating points assists rebounds 0 90 25 5 8.0 1 85 20 7 NaN 2 82 14 7 10.0 3 88 16 8 6.0 4 94 27 5 6.0 5 90 20 7 9.0 6 76 12 6 6.0 7 75 15 9 10.0 8 87 14 9 10.0 9 86 19 5 7.07
Example 1: Find the Sum of Each Row
We can find the sum of each row in the DataFrame by using the following syntax:
df.sum(axis=1)
0 128.0
1 112.0
2 113.0
3 118.0
4 132.0
5 126.0
6 100.0
7 109.0
8 120.0
9 117.0
dtype: float64
The output tells us:
- The sum of values in the first row is 128.
- The sum of values in the second row is 112.
- The sum of values in the third row is 113.
And so on.
Example 2: Place the Row Sums in a New Column
We can use the following code to add a column to our DataFrame to hold the row sums:
#define new DataFrame column 'row_sum' as the sum of each row df['row_sum'] = df.sum(axis=1) #view DataFrame df rating points assists rebounds row_sum 0 90 25 5 8.0 128.0 1 85 20 7 NaN 112.0 2 82 14 7 10.0 113.0 3 88 16 8 6.0 118.0 4 94 27 5 6.0 132.0 5 90 20 7 9.0 126.0 6 76 12 6 6.0 100.0 7 75 15 9 10.0 109.0 8 87 14 9 10.0 120.0 9 86 19 5 7.0 117.0
Example 3: Find the Row Sums for a Short List of Specific Columns
We can use the following code to find the row sum for a short list of specific columns:
#define new DataFrame column as sum of points and assists columns df['sum_pa'] = df['points'] + df['assists'] #view DataFrame df rating points assists rebounds sum_pa 0 90 25 5 8.0 30 1 85 20 7 NaN 27 2 82 14 7 10.0 21 3 88 16 8 6.0 24 4 94 27 5 6.0 32 5 90 20 7 9.0 27 6 76 12 6 6.0 18 7 75 15 9 10.0 24 8 87 14 9 10.0 23 9 86 19 5 7.0 24
Example 4: Find the Row Sums for a Long List of Specific Columns
We can use the following code to find the row sum for a longer list of specific columns:
#define col_list as a list of all DataFrame column names col_list= list(df) #remove the column 'rating' from the list col_list.remove('rating') #define new DataFrame column as sum of rows in col_list df['new_sum'] = df[col_list].sum(axis=1) #view DataFrame df rating points assists rebounds new_sum 0 90 25 5 8.0 38.0 1 85 20 7 NaN 27.0 2 82 14 7 10.0 31.0 3 88 16 8 6.0 30.0 4 94 27 5 6.0 38.0 5 90 20 7 9.0 36.0 6 76 12 6 6.0 24.0 7 75 15 9 10.0 34.0 8 87 14 9 10.0 33.0 9 86 19 5 7.0 31.0
You can find the complete documentation for the pandas sum() function .