How to Sum Specific Rows in Pandas (With Examples)


You can use the following methods to find the sum of specific rows in a pandas DataFrame:

Method 1: Sum Specific Rows by Index

#sum rows in index positions 0, 1, and 4
df.iloc[[0, 1, 4]].sum()

Method 2: Sum Specific Rows by Label

#sum rows with index labels 'A', 'B', and 'E'
df.loc[['A', 'B', 'E']].sum() 

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [28, 17, 19, 14, 23, 26, 5],
                   'rebounds': [5, 6, 4, 7, 14, 12, 9],
                   'assists': [10, 13, 7, 8, 4, 5, 8]})

#set index
df = df.set_index([pd.Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'])])

#view DataFrame
print(df)

   points  rebounds  assists
A      28         5       10
B      17         6       13
C      19         4        7
D      14         7        8
E      23        14        4
F      26        12        5
G       5         9        8

Example 1: Sum Specific Rows by Index

The following code shows how to sum the values in the rows with index values 0, 1, and 4 for each column in the DataFrame:

#sum rows in index positions 0, 1, and 4
df.iloc[[0, 1, 4]].sum()

points      68
rebounds    25
assists     27
dtype: int64

From the output we can see:

  • The sum of rows with index values 0, 1, and 4 for the points column is 68.
  • The sum of rows with index values 0, 1, and 4 for the rebounds column is 25.
  • The sum of rows with index values 0, 1, and 4 for the assists column is 27.

Also note that you can sum a specific range of rows by using the following syntax:

#sum rows in index positions between 0 and 4
df.iloc[0:4].sum()

points      78
rebounds    22
assists     38
dtype: int64

From the output we can see the sum of the rows with index values between 0 and 4 (not including 4) for each of the columns in the DataFrame.

Example 2: Sum Specific Rows by Label

The following code shows how to sum the values in the rows with index labels ‘A’, ‘B’, and ‘E’ for each column in the DataFrame:

#sum rows with index labels 'A', 'B', and 'E'
df.loc[['A', 'B', 'E']].sum()

points      68
rebounds    25
assists     27
dtype: int64

From the output we can see:

  • The sum of rows with index values ‘A’, ‘B’, and ‘E’ for the points column is 68.
  • The sum of rows with index values ‘A’, ‘B’, and ‘E’ for the rebounds column is 25.
  • The sum of rows with index values ‘A’, ‘B’, and ‘E’ for the assists column is 27.

Related: The Difference Between loc vs. iloc in Pandas

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Perform a SUMIF Function in Pandas
How to Perform a GroupBy Sum in Pandas
How to Sum Columns Based on a Condition in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *