This tutorial shows how to sum rows in a dataframe using Pandas in Python.
Example 1: Use sum() for all rows
import pandas as pd
brand_data = {'Brand': ['BrandA', 'BrandB','BrandA','BrandC','BrandA'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Number':[20,30,25,20,20]}
brand_data=pd.DataFrame(data=brand_data)
print(brand_data)
Brand Location Number 0 BrandA CA 20 1 BrandB CA 30 2 BrandA NY 25 3 BrandC MA 20 4 BrandA CA 20
brand_data["Number"].sum( skipna = True)
115
brand_data["Number"].sum(axis=0,skipna = True)
115
Example 2: Use sum() for selected rows
The following code shows how to use sum()
for the first two rows.
# sum of the first two rows for the column of "Number"
sum_result=brand_data.loc[0:1,"Number"].sum()
print(sum_result)
50
# sum of the first two rows for the column of "Number"
sum_result=brand_data["Number"].loc[0:1].sum()
print(sum_result)
50
You can use minus notation to sum the last few rows. The following is to sum the last two rows.
# the following code will sum last two numbers, namely the last second
sum_result=brand_data["Number"].iloc[-2:].sum()
print(sum_result)
40
If you add -1 within iloc[]
, it will only sum the last second number, and ignore the last number.
# the following code will only sum one number, namely the last second
sum_result=brand_data["Number"].iloc[-2:-1].sum()
print(sum_result)
20