January 28, 2020
Start a python program that will use Pandas and Matplotlib
from matplotlib import pyplot as plt
import pandas as pd
Read a csv file and set it as data to be plotted
orders = pd.read_csv(‘orders.csv’)
Take the date column and use the month to create a new df
orders[‘math’] = orders.date.apply(lambda x: x.split(‘-’)[0])
This takes the first values before the ‘-’ in the date and makes a new column
Get the average order by month and save it to a variable
avg_order = orders.date.groupby(‘month’).price.mean( ).reset_index( )
Get the standard deviation for the orders by month and save it to a variable
std_order = orders.date.groupby(‘month’).price.mean( ).reset_index( )
Create a set of axes using plt.subplot and save them to a variable
ax = plt.subplot( )
Create a variable with the average prices in it by selecting the column price from avg_order. Save this to bar_heights
bar_heights = avg_order.price
Create a variable with the standard deviation of prices in it by selecting the column price for std_order. Save this to bar_errors.
bar_errors = std_order.price
Create a bar chart to share this data. The height of each bar should come from bar heights. Use the standard deviations in bar_errors as the yerr. The error capsize should be 5. Label each bar with the name of the month. Label the y axis. Give the plot a descriptive title.
plt.bar(range(len(bar_heights)), bar_heights, yerr=bar_errors, capsize = 5)
ax.set_xticks(range(len(bar_heights)))
ax.set_xticklabels([‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’])
plt.ylabel(‘Average Order Amount’)
plt.title(‘Order Amount over Time’)
plt.show( )