Pandas Sum DataFrame Rows With Examples

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:February 16, 2023
Spread the love

To sum Pandas DataFrame rows (given selected multiple rows) use sum() function. The Pandas DataFrame.sum() function returns the sum of the values for the requested axis, In order to calculate the sum of rows use the default param axis=0, and to get the sum of columns use axis=1. In this article, I will explain how to sum pandas DataFrame for given rows with examples.

1. Quick Examples of Sum DataFrame Rows

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


# Below are quick example
# Example 1: Using sum() to Sum the rows of each column
df1 = df.sum()
 
# Example 2: Get sum of all rows as a new row in Dataframe
sum = df.sum()
sum.name = 'Sum'
# Assign sum of all rows of DataFrame as a new Row
df = df.append(sum.transpose())
 
# Example 3: Get sum of first 2 rows of DataFrame 
sum = df.iloc[0:2].sum()

# Example 4: Get sum of 3 rows (selected by index labels)
sum = df.loc[['r1', 'r3', 'r4']].sum() 

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


import pandas as pd
studentdetails = {
       "Studentname":["Ram", "Sam", "Scott", "Ann", "John"],
       "Mathematics" :[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)

Yield below output.


# Output:
  Studentname  Mathematics  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 Rows

Use DataFrame.sum() to get the sum/total of a Pandas DataFrame for both rows and columns. By default, this function takes axis = 0 and adds all the rows of each column and returns the Pandas Series where the values are the sum of all rows over the columns. If we pass the axis param as '1' to this function, we can get a sum of all columns.


# Using sum() to Sum the rows of each column
df1 = df.sum()
print(df1)

Yields below output. Note that for string columns, it just concatenates the values from columns. From our example Studentname is a string column.


# Output:
Studentname    RamSamScottAnnJohn
Mathematics                   420
Science                       425
English                       420
dtype: object

3. Add Sum of Rows as an Index of Pandas DataFrame

If you notice the above output, the actual row values that are part of the sum are not returned by DataFrame.sum() function however, you can get all rows including the sum row by assigning the DataFrame.sum() to a DataFrame row. Lets add a row 'Sum' which is the sum of rows for each column. We can add this row to the DataFrame with the help of the pandas.append() and pd.transpose() functions.


# Get sum of all rows as a new row in Dataframe
sum = df.sum()
sum.name = 'Sum'
# Assign sum of all rows of DataFrame as a new Row
df = df.append(sum.transpose())
print(df)

Yields below output. Here, series.name is used to set a name to the index.


# Output:
           Studentname  Mathematics  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
Sum  RamSamScottAnnJohn          420      425      420

As we can see from the above, sum row has been added to the Pandas DataFrame with index sum.

4. Pandas Sum Multiple Rows using iloc[]

We can also calculate sum for the specified multiple rows of the dataframe using the index range of the DataFrame.iloc[] property. This property will select a specified portion of rows and add them using the sum() function. Then, we will get the sum of specified rows in the form of Series.


# Get sum of first 2 rows of DataFrame 
sum = df.iloc[0:2].sum()
print(sum)

Yields below output.


# Output
Studentname    RamSam
Mathematics       170
Science           180
English           175
dtype: object

5. Pandas Sum Multiple Rows using loc[]

By using DataFrame.loc[] function, select the rows by labels and then use the sum() function to calculate the sum of rows. Let’s use the loc[] attribute and select specified rows then call the sum() function, this syntax will return the sum of specified rows in the form of a Series.


# Get sum of 3 DataFrame rows (selected by index labels)
sum = df.loc[['r1', 'r3', 'r4']].sum()
print(sum)

Yields below output.


# Output
Studentname    RamScottAnn
Mathematics            235
Science                255
English                240
dtype: object

6. Conclusion

In this article, I have explained how to sum Pandas DataFrame rows (all, multiple rows) using the sum()function and also I explained how to add Pandas rows for only selected rows using iloc[], loc[] attributes with several well defined examples.

References

Leave a Reply

You are currently viewing Pandas Sum DataFrame Rows With Examples