• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:9 mins read
You are currently viewing Pandas Sum DataFrame Rows With Examples

To sum all Pandas DataFrame rows or given selected rows use the 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 the quick examples.

# 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 the results. Our DataFrame contains column names studentnamemathematicsscience and english.


# Create DataFrame
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("Create DataFrame:\n", df)

Yield below output.

pandas sum rows

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("Get sum of all rows in a DataFrame:\n", df1)

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

pandas sum rows

3. Add the 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 the  DataFrame.sum() function however, you can get all rows including the sum row by assigning this function to a DataFrame row. Let’s 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("Add sum column to DataFrame:\n", df)

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


# Output:
# Add sum column to DataFrame:
           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, the sum row has been added to the Pandas DataFrame with index sum.

4. Pandas Sum Specified Rows using iloc[]

We can also calculate the 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 a Series.


# Get sum of first 2 rows of DataFrame 
sum = df.iloc[0:2].sum()
print("Get sum of specified rows:\n", sum)

Yields below output.


# Output:
# Get sum of specified rows:
Studentname    RamSam
Mathematics       170
Science           180
English           175
dtype: object

5. Pandas Sum Specified 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("Get sum of specified rows:\n", sum)

Yields below output.


# Output:
# Get sum of specified rows:
Studentname    RamScottAnn
Mathematics            235
Science                255
English                240
dtype: object

6. Conclusion

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

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium