Q#59 Chipotle total revenue per item

Suppose you are given a data set of Chipotle orders. Using these data, can you make a plot showing total revenue per menu item?

TRY IT YOURSELF

ANSWER

This question tests our ability to wrangle data and plot it using Python, more specifically the data science friendly package Pandas.

The first step is to look at the data you are about to use and identify two potential problems. First, the price has a ‘$’ sign and it will load in as an object and second, this data is tab separated. So, let’s load the data using the standard .read_csv() function in Pandas but with the extra argument sep=’\t’ for tab separation. Next, lets address the ‘$’ sign issue for the item_price column. We can use the .str.replace() function with the arguments ‘$’ and ‘“”’ and then chain it with .astype(‘float’) to make it numeric.

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv', sep = '\t')
df['item_price'] = df['item_price'].str.replace('$', '').astype('float')

Now, we are ready to create the plot. To do this, we can use the .groupby() function to group by the item_name and then take the sum of the item_price with .sum() and sort the result with .sort_values(). Finally, let’s chain this with .plot() and the argument kind = ‘barh’ for a horizontal bar plot and we are done!

df.groupby('item_name').item_price.sum().sort_values().plot(kind = 'barh', figsize = (12,15))

--

--

--

Data Science tutorial working through solutions to Data Science Interview Questions

Recommended from Medium

A simple guide explaining how to use scikit learn for linear regression

LEZME MINIGAME 1: THE PRICE IS RIGHT — OFFICIALLY STARTED

OLAP is over! What’s next?

The World in 2022

Requesting Assistance: Winter Research from Golf Course Superintendents — Univ. of Wisconsin

Big Data is a strategic industrial asset

Stain estimation on microscopy whole slide images

Linear Regression With One Variable

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abish Pius

Abish Pius

Data Science Professional who teaches with unflashy, simple to understand python code.

More from Medium

Day 19 of #66DaysOfDataChallenge

#66DaysofData – 44 Days In

Data Pipeline using Python

Scraping Data using YouTube API