In [1]:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'shopping_cart_project.settings'
from shopping_cart.models import *
from django.shortcuts import *

In [2]:
import pandas as pd

In [26]:
shipping = {'ground': 4.35, 'snail': 14.43, 'owl': 5.00, 'teleporter':324.04, 'pony_express':45.28}

In [27]:
cart =[
{'item_id': u'4', 'price': u'10.5', 'name': u'Eye Patch', 'quantity': u'1', 'shipping':'ground'}, 
{'item_id': u'1', 'price': u'10000', 'name': u'Time', 'quantity': u'1', 'shipping':'ground'}, 
{'item_id': u'7', 'price': u'800', 'name': u'Fender Strat', 'quantity': u'1','shipping':'ground'}, 
{'item_id': u'7', 'price': u'800', 'name': u'Fender Strat', 'quantity': u'1', 'shipping':'ground'}]

In [28]:
df = pd.DataFrame(cart, dtype=np.float16)

In [29]:
df


Out[29]:
item_id name price quantity shipping
0 4 Eye Patch 10.5 1 ground
1 1 Time 10000.0 1 ground
2 7 Fender Strat 800.0 1 ground
3 7 Fender Strat 800.0 1 ground

In [30]:
tax = .08

In [31]:
df['shipping_costs'] = df.shipping.apply(lambda x: shipping[x])
df['tax'] = (((df.price * df.quantity)+ df.shipping_costs) * tax)
df['total_price'] = ((df.price * df.quantity) + df.tax + df.shipping_costs)

In [32]:
df


Out[32]:
item_id name price quantity shipping shipping_costs tax total_price
0 4 Eye Patch 10.5 1 ground 4.35 0.840332 15.69375
1 1 Time 10000.0 1 ground 4.35 800.000000 10804.35000
2 7 Fender Strat 800.0 1 ground 4.35 64.000000 868.35000
3 7 Fender Strat 800.0 1 ground 4.35 64.000000 868.35000

In [35]:



Out[35]:
11608.0

In [39]:
total = df.total_price.sum()
a = [((df.price * df.quantity).sum()/ total), 
     df.shipping_costs.sum() / total,
     df.tax.sum() / total]

In [46]:
a


Out[46]:
[0.92444348878267091, 0.0013857095714006266, 0.073984148955815066]

In [15]:
df.to_dict()


Out[15]:
{'item_id': {0: 4.0, 1: 1.0, 2: 7.0, 3: 7.0},
 'name': {0: u'Eye Patch', 1: u'Time', 2: u'Fender Strat', 3: u'Fender Strat'},
 'price': {0: 10.5, 1: 10000.0, 2: 800.0, 3: 800.0},
 'quantity': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
 'shipping': {0: 'ground', 1: 'ground', 2: 'ground', 3: 'ground'},
 'shipping_costs': {0: 4.3499999999999996,
  1: 4.3499999999999996,
  2: 4.3499999999999996,
  3: 4.3499999999999996},
 'tax': {0: 0.84033, 1: 800.0, 2: 64.0, 3: 64.0},
 'total_price': {0: 15.69375,
  1: 10804.35,
  2: 868.35000000000002,
  3: 868.35000000000002}}

In [ ]:
import pandas as pd
shipping = {'ground': 4.35, 'snail': 14.43, 'owl': 5.00, 'teleporter':324.04, 'pony_express':45.28}
tax = .08

df = pd.DataFrame(cart, dtype=np.float16)
df['shipping_costs'] = df.shipping.apply(lambda x: shipping[x])
df['tax'] = ((df.price * df.quantity) * tax)
df['total_price'] = ((df.price * df.quantity) + df.tax + df.shipping_costs)
df.to_json()