SUM

11/12/16

The SUM function adds values, whether they be individual values, cell references, cell ranges, or a mix of all three.

In [1]:
import pandas as pd
In [6]:
df = pd.read_csv('sample_data.csv')
In [33]:
df
Out[33]:
Name Date Item Product Category Time (hrs) Amt Picked (lbs) Sale Value ($/lb)
0 Lucas 1/5/16 Green Apples Apples 2 5 2.5
1 Valter 1/5/16 European Pear Pears 4 10 2.0
2 Erik 1/8/16 Red Apples Apples 2 3 2.1
3 Georg 2/1/16 Asian Pear Pears 8 15 3.8
4 Lucas 2/10/16 Red Apples Apples 6 6 3.6

Summing column(s)

Summing all columns


Will not make sense for string-typed columns

In [34]:
df.sum()
Out[34]:
Name                                         LucasValterErikGeorgLucas
Date                                   1/5/161/5/161/8/162/1/162/10/16
Item                 Green ApplesEuropean PearRed ApplesAsian PearR...
Product Category                          ApplesPearsApplesPearsApples
Time (hrs)                                                          22
Amt Picked (lbs)                                                    39
Sale Value ($/lb)                                                   14
dtype: object

Summing multiple columns (opt-in method)

In [39]:
columns = ['Time (hrs)', 'Amt Picked (lbs)']
df[columns].sum()
Out[39]:
Time (hrs)          22
Amt Picked (lbs)    39
dtype: int64

Summing multiple columns (opt-out method)

In [72]:
columns = list(df)
columns
Out[72]:
['Name',
 'Date',
 'Item',
 'Product Category',
 'Time (hrs)',
 'Amt Picked (lbs)',
 'Sale Value ($/lb)']
In [73]:
removelist = ['Name', 'Date', 'Item', 'Product Category']
newlist = [value for value in columns if value not in removelist]
df[newlist].sum()
Out[73]:
Time (hrs)           22
Amt Picked (lbs)     39
Sale Value ($/lb)    14
dtype: float64

Summing one specific column

All rows

In [41]:
df['Amt Picked (lbs)'].sum()
Out[41]:
39

Range of rows

In [81]:
df.ix[0:2]['Amt Picked (lbs)'].sum()
Out[81]:
18

Summing row(s)


Useful if the dataset has been transposed

In [42]:
df.T
Out[42]:
0 1 2 3 4
Name Lucas Valter Erik Georg Lucas
Date 1/5/16 1/5/16 1/8/16 2/1/16 2/10/16
Item Green Apples European Pear Red Apples Asian Pear Red Apples
Product Category Apples Pears Apples Pears Apples
Time (hrs) 2 4 2 8 6
Amt Picked (lbs) 5 10 3 15 6
Sale Value ($/lb) 2.5 2 2.1 3.8 3.6
In [24]:
df_alt = df.T

Summing all rows

In [28]:
df_alt.sum(axis=1)
Out[28]:
Name                                         LucasValterErikGeorgLucas
Date                                   1/5/161/5/161/8/162/1/162/10/16
Item                 Green ApplesEuropean PearRed ApplesAsian PearR...
Product Category                          ApplesPearsApplesPearsApples
Time (hrs)                                                          22
Amt Picked (lbs)                                                    39
Sale Value ($/lb)                                                   14
dtype: object

Summing specific rows (opt-in method)

In [26]:
rows = ['Time (hrs)', 'Amt Picked (lbs)']
In [27]:
df_alt.ix[rows].sum(axis=1)
Out[27]:
Time (hrs)          22
Amt Picked (lbs)    39
dtype: float64