0

Can someone provide sample code of what they would do?

Johnn-1231
  • 85
  • 1
  • 1
  • 5
  • Does this answer your question? [converting currency with $ to numbers in Python pandas](https://stackoverflow.com/questions/32464280/converting-currency-with-to-numbers-in-python-pandas) – Mark Plotnick Jun 06 '20 at 01:58

2 Answers2

0

This should do:

import pandas, statistics
from decimal import *

# load the .tsv straight from the inter web 
data = pandas.read_csv('https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv', sep='\t')

# item_price to Decimal (removing $)
data['item_price'] = data['item_price'].map(lambda x: Decimal(x.strip('$')))

# calculating total for each orders, format {order_id: total,}
total_for_orders = {}
for order_id, group in data.groupby('order_id'):
    total_for_orders[order_id] = sum(group['quantity'] * group['item_price'])

# mean of all the values in the dictionary
mean_all_orders = statistics.mean(total_for_orders.values())

print('Mean of all Orders:', mean_all_orders)

Ouput:

Mean of all Orders: 21.39423118865866957470010905
Sy Ker
  • 2,047
  • 1
  • 4
  • 20
  • This answer is mostly ok, except in general it is not a good idea to represent currency using float due to issues with rounding. – lightalchemist Jun 06 '20 at 03:15
  • What do you mean ? – Sy Ker Jun 06 '20 at 03:17
  • It is a common mistake to represent currency using float. There are dedicated packages for representing currency and they should be used for any non-trivial (i.e., industrial grade) applications. See https://stackoverflow.com/questions/1406737/what-class-to-use-for-money-representation and google for related packages to get an idea. – lightalchemist Jun 06 '20 at 03:34
  • I've updated the answer with your very welcome suggestion. So much to learn... – Sy Ker Jun 06 '20 at 03:44
0
from decimal import Decimal

def custom_average(x):
    return (sum(x) / len(x)).quantize(Decimal('0.01'), rounding=decimal.ROUND_UP)

df = pandas.read_csv('https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv', sep='\t')

# Represent item prices as Decimal object
df.item_price = df.item_price.map(lambda x: Decimal(x.lstrip('$')))

# Total price of order
df['total'] = df.item_price * df.quantity

# Group by order id and compute average 
avg_order_price = df.groupby('order_id').agg({'total' : custom_average })

# Display first 10 orders
avg_order_price.head(10)

produces the result

          total
order_id
1          2.89
2         33.96
3          6.34
4         10.50
5          6.85
6          8.75
7          7.85
8          5.44
9          6.43
10         6.60

The average cost of the orders:

custom_average(df.total)

produces

Decimal('8.49')
lightalchemist
  • 10,031
  • 4
  • 47
  • 55