diff --git a/01_Getting_&_Knowing_Your_Data/Chipotle/Exercise_with_Solutions.ipynb b/01_Getting_&_Knowing_Your_Data/Chipotle/Exercise_with_Solutions.ipynb index a5467d269..9a42401be 100644 --- a/01_Getting_&_Knowing_Your_Data/Chipotle/Exercise_with_Solutions.ipynb +++ b/01_Getting_&_Knowing_Your_Data/Chipotle/Exercise_with_Solutions.ipynb @@ -473,7 +473,26 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 13. How much was the revenue for the period in the dataset?" + "### Step 13. Turn the item price into a float" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "dollarizer = lambda x: float(x[1:-1])\n", + "chipo.item_price = chipo.item_price.apply(dollarizer)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Step 14. How much was the revenue for the period in the dataset?" ] }, { @@ -484,28 +503,25 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "The revenue of the period was: $34500.16\n" - ] + "data": { + "text/plain": [ + "34500.16000000046" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# create a list of prices\n", - "prices = [float(value[1:-1]) for value in chipo.item_price] # strip the dollar sign and trailing space\n", - "\n", - "total = sum(prices) #sum the items\n", - "\n", - "\n", - "print(\"The revenue of the period was: \" + \"${0:.2f}\".format(total))" + "chipo.item_price.sum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 14. How many orders were made in the period?" + "### Step 15. How many orders were made in the period?" ] }, { @@ -527,15 +543,14 @@ } ], "source": [ - "orders = len((set([order for order in chipo.order_id])))\n", - "orders" + "chipo.order_id.value_counts().count()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 15. What is the average amount per order?" + "### Step 16. What is the average amount per order?" ] }, { @@ -546,24 +561,30 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "The average order is: $18.81\n" - ] + "data": { + "text/plain": [ + "18.811428571428689" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "avg_order = total / orders\n", + "order_grouped = chipo.groupby(by=['order_id']).sum()\n", + "order_grouped.mean()['item_price']\n", + "\n", + "# Or \n", "\n", - "print \"The average order is: $%s\" % round(avg_order, 2)" + "#chipo.groupby(by=['order_id']).sum().mean()['item_price']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 16 How many different items are sold?" + "### Step 17. How many different items are sold?" ] }, { @@ -585,7 +606,7 @@ } ], "source": [ - "len(chipo.item_name.unique())" + "chipo.item_name.value_counts().count()" ] } ], diff --git a/01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb b/01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb index 3f4398901..4808ae47e 100644 --- a/01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb +++ b/01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb @@ -197,7 +197,23 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 13. How much was the revenue for the period in the dataset?" + "### Step 13. Turn the item price into a float" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Step 14. How much was the revenue for the period in the dataset?" ] }, { @@ -213,7 +229,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 14. How many orders were made in the period?" + "### Step 15. How many orders were made in the period?" ] }, { @@ -229,7 +245,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 15. What is the average amount per order?" + "### Step 16. What is the average amount per order?" ] }, { @@ -245,7 +261,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 16. How many different items are sold?" + "### Step 17. How many different items are sold?" ] }, { diff --git a/01_Getting_&_Knowing_Your_Data/Chipotle/Solutions.ipynb b/01_Getting_&_Knowing_Your_Data/Chipotle/Solutions.ipynb index 7c37b2750..364eeb2cd 100644 --- a/01_Getting_&_Knowing_Your_Data/Chipotle/Solutions.ipynb +++ b/01_Getting_&_Knowing_Your_Data/Chipotle/Solutions.ipynb @@ -441,7 +441,23 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 13. How much was the revenue for the period in the dataset?" + "### Step 13. Turn the item price into a float" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Step 14. How much was the revenue for the period in the dataset?" ] }, { @@ -452,11 +468,14 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "The revenue of the period was: $34500.16\n" - ] + "data": { + "text/plain": [ + "34500.16000000046" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" } ], "source": [] @@ -465,7 +484,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 14. How many orders were made in the period?" + "### Step 15. How many orders were made in the period?" ] }, { @@ -492,7 +511,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 15. What is the average amount per order?" + "### Step 16. What is the average amount per order?" ] }, { @@ -503,11 +522,14 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "The average order is: $18.81\n" - ] + "data": { + "text/plain": [ + "18.811428571428689" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" } ], "source": [] @@ -516,7 +538,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Step 16 How many different items are sold?" + "### Step 17. How many different items are sold?" ] }, {