Skip to content

Commit 1849f10

Browse files
authored
Merge pull request guipsamora#5 from mtd91429/chipotle
changed solutions to utilize pandas functionality @mtd91429 thank you for the contribution.
2 parents 6c5947f + 5b6e7ff commit 1849f10

File tree

3 files changed

+103
-44
lines changed

3 files changed

+103
-44
lines changed

01_Getting_&_Knowing_Your_Data/Chipotle/Exercise_with_Solutions.ipynb

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,26 @@
473473
"cell_type": "markdown",
474474
"metadata": {},
475475
"source": [
476-
"### Step 13. How much was the revenue for the period in the dataset?"
476+
"### Step 13. Turn the item price into a float"
477+
]
478+
},
479+
{
480+
"cell_type": "code",
481+
"execution_count": null,
482+
"metadata": {
483+
"collapsed": true
484+
},
485+
"outputs": [],
486+
"source": [
487+
"dollarizer = lambda x: float(x[1:-1])\n",
488+
"chipo.item_price = chipo.item_price.apply(dollarizer)"
489+
]
490+
},
491+
{
492+
"cell_type": "markdown",
493+
"metadata": {},
494+
"source": [
495+
"### Step 14. How much was the revenue for the period in the dataset?"
477496
]
478497
},
479498
{
@@ -484,28 +503,25 @@
484503
},
485504
"outputs": [
486505
{
487-
"name": "stdout",
488-
"output_type": "stream",
489-
"text": [
490-
"The revenue of the period was: $34500.16\n"
491-
]
506+
"data": {
507+
"text/plain": [
508+
"34500.16000000046"
509+
]
510+
},
511+
"execution_count": 130,
512+
"metadata": {},
513+
"output_type": "execute_result"
492514
}
493515
],
494516
"source": [
495-
"# create a list of prices\n",
496-
"prices = [float(value[1:-1]) for value in chipo.item_price] # strip the dollar sign and trailing space\n",
497-
"\n",
498-
"total = sum(prices) #sum the items\n",
499-
"\n",
500-
"\n",
501-
"print(\"The revenue of the period was: \" + \"${0:.2f}\".format(total))"
517+
"chipo.item_price.sum()"
502518
]
503519
},
504520
{
505521
"cell_type": "markdown",
506522
"metadata": {},
507523
"source": [
508-
"### Step 14. How many orders were made in the period?"
524+
"### Step 15. How many orders were made in the period?"
509525
]
510526
},
511527
{
@@ -527,15 +543,14 @@
527543
}
528544
],
529545
"source": [
530-
"orders = len((set([order for order in chipo.order_id])))\n",
531-
"orders"
546+
"chipo.order_id.value_counts().count()"
532547
]
533548
},
534549
{
535550
"cell_type": "markdown",
536551
"metadata": {},
537552
"source": [
538-
"### Step 15. What is the average amount per order?"
553+
"### Step 16. What is the average amount per order?"
539554
]
540555
},
541556
{
@@ -546,24 +561,30 @@
546561
},
547562
"outputs": [
548563
{
549-
"name": "stdout",
550-
"output_type": "stream",
551-
"text": [
552-
"The average order is: $18.81\n"
553-
]
564+
"data": {
565+
"text/plain": [
566+
"18.811428571428689"
567+
]
568+
},
569+
"execution_count": 4,
570+
"metadata": {},
571+
"output_type": "execute_result"
554572
}
555573
],
556574
"source": [
557-
"avg_order = total / orders\n",
575+
"order_grouped = chipo.groupby(by=['order_id']).sum()\n",
576+
"order_grouped.mean()['item_price']\n",
577+
"\n",
578+
"# Or \n",
558579
"\n",
559-
"print \"The average order is: $%s\" % round(avg_order, 2)"
580+
"#chipo.groupby(by=['order_id']).sum().mean()['item_price']"
560581
]
561582
},
562583
{
563584
"cell_type": "markdown",
564585
"metadata": {},
565586
"source": [
566-
"### Step 16 How many different items are sold?"
587+
"### Step 17. How many different items are sold?"
567588
]
568589
},
569590
{
@@ -585,7 +606,7 @@
585606
}
586607
],
587608
"source": [
588-
"len(chipo.item_name.unique())"
609+
"chipo.item_name.value_counts().count()"
589610
]
590611
}
591612
],

01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,23 @@
197197
"cell_type": "markdown",
198198
"metadata": {},
199199
"source": [
200-
"### Step 13. How much was the revenue for the period in the dataset?"
200+
"### Step 13. Turn the item price into a float"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"metadata": {
207+
"collapsed": true
208+
},
209+
"outputs": [],
210+
"source": []
211+
},
212+
{
213+
"cell_type": "markdown",
214+
"metadata": {},
215+
"source": [
216+
"### Step 14. How much was the revenue for the period in the dataset?"
201217
]
202218
},
203219
{
@@ -213,7 +229,7 @@
213229
"cell_type": "markdown",
214230
"metadata": {},
215231
"source": [
216-
"### Step 14. How many orders were made in the period?"
232+
"### Step 15. How many orders were made in the period?"
217233
]
218234
},
219235
{
@@ -229,7 +245,7 @@
229245
"cell_type": "markdown",
230246
"metadata": {},
231247
"source": [
232-
"### Step 15. What is the average amount per order?"
248+
"### Step 16. What is the average amount per order?"
233249
]
234250
},
235251
{
@@ -245,7 +261,7 @@
245261
"cell_type": "markdown",
246262
"metadata": {},
247263
"source": [
248-
"### Step 16. How many different items are sold?"
264+
"### Step 17. How many different items are sold?"
249265
]
250266
},
251267
{

01_Getting_&_Knowing_Your_Data/Chipotle/Solutions.ipynb

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,23 @@
441441
"cell_type": "markdown",
442442
"metadata": {},
443443
"source": [
444-
"### Step 13. How much was the revenue for the period in the dataset?"
444+
"### Step 13. Turn the item price into a float"
445+
]
446+
},
447+
{
448+
"cell_type": "code",
449+
"execution_count": null,
450+
"metadata": {
451+
"collapsed": true
452+
},
453+
"outputs": [],
454+
"source": []
455+
},
456+
{
457+
"cell_type": "markdown",
458+
"metadata": {},
459+
"source": [
460+
"### Step 14. How much was the revenue for the period in the dataset?"
445461
]
446462
},
447463
{
@@ -452,11 +468,14 @@
452468
},
453469
"outputs": [
454470
{
455-
"name": "stdout",
456-
"output_type": "stream",
457-
"text": [
458-
"The revenue of the period was: $34500.16\n"
459-
]
471+
"data": {
472+
"text/plain": [
473+
"34500.16000000046"
474+
]
475+
},
476+
"execution_count": 130,
477+
"metadata": {},
478+
"output_type": "execute_result"
460479
}
461480
],
462481
"source": []
@@ -465,7 +484,7 @@
465484
"cell_type": "markdown",
466485
"metadata": {},
467486
"source": [
468-
"### Step 14. How many orders were made in the period?"
487+
"### Step 15. How many orders were made in the period?"
469488
]
470489
},
471490
{
@@ -492,7 +511,7 @@
492511
"cell_type": "markdown",
493512
"metadata": {},
494513
"source": [
495-
"### Step 15. What is the average amount per order?"
514+
"### Step 16. What is the average amount per order?"
496515
]
497516
},
498517
{
@@ -503,11 +522,14 @@
503522
},
504523
"outputs": [
505524
{
506-
"name": "stdout",
507-
"output_type": "stream",
508-
"text": [
509-
"The average order is: $18.81\n"
510-
]
525+
"data": {
526+
"text/plain": [
527+
"18.811428571428689"
528+
]
529+
},
530+
"execution_count": 4,
531+
"metadata": {},
532+
"output_type": "execute_result"
511533
}
512534
],
513535
"source": []
@@ -516,7 +538,7 @@
516538
"cell_type": "markdown",
517539
"metadata": {},
518540
"source": [
519-
"### Step 16 How many different items are sold?"
541+
"### Step 17. How many different items are sold?"
520542
]
521543
},
522544
{

0 commit comments

Comments
 (0)