<jupyter_start><jupyter_text>**Exercise 8.03**<jupyter_code>#orders made by each customer

ord_cust = retail.groupby(by = ['cust_id', 'country'], as_index = False)['invoice'].count()
ord_cust.head(10)
plt.subplots(figsize = (15, 6))
oc = plt.plot(ord_cust.cust_id, ord_cust.invoice)
plt.xlabel('Customer ID')
plt.ylabel('Number of Orders')
plt.title('Number of Orders made by Customers')
plt.show()

ord_cust.describe()
# 5 customers who ordered the most number of times
ord_cust.sort_values(by = 'invoice', ascending = False).head()
ord_cust.sort_values(by = 'invoice', ascending = False).tail()
spent_cust = retail.groupby(by = ['cust_id', 'country', 'quantity', 'unit_price'], as_index = False)['spent'].sum()
spent_cust.head()
plt.subplots(figsize = (15, 6))
sc = plt.plot(spent_cust.cust_id, spent_cust.spent)
plt.xlabel('Customer ID')
plt.ylabel('Total Amount Spent')
plt.title('Amount Spent by Customers')
plt.show()
spent_cust.sort_values(by = 'spent', ascending = False).head()
spent_cust.sort_values(by = 'spent', ascending = False).tail()
ord_month = retail.groupby(['invoice'])['year_month'].unique().value_counts().sort_index()
ord_month

om = ord_month.plot(kind='bar', figsize = (15, 6))
om.set_xlabel('Month')
om.set_ylabel('Number of Orders')
om.set_title('Orders per Month')
om.set_xticklabels(('Dec 09', 'Jan 10', 'Feb 10', 'Mar 10', 'Apr 10', 'May 10', 
                           'Jun 10', 'Jul 10', 'Aug 10', 'Sep 10', 'Oct 10', 'Nov 10', 'Dec 10'), rotation = 'horizontal')
plt.show()
ord_day = retail.groupby('invoice')['day'].unique().value_counts().sort_index()
ord_day

od = ord_day.plot(kind='bar', figsize = (15, 6))
od.set_xlabel('Day of the Month')
od.set_ylabel('Number of Orders')
od.set_title('Orders per Day of the Month')
od.set_xticklabels(labels = [i for i in range (1, 32)], rotation = 'horizontal')
plt.show()

ord_dayofweek = retail.groupby('invoice')['day_of_week'].unique().value_counts().sort_index()
ord_dayofweek

odw = ord_dayofweek.plot(kind='bar', figsize = (15, 6))
odw.set_xlabel('Day of the Week')
odw.set_ylabel('Number of Orders')
odw.set_title('Orders per Day of the Week')
odw.set_xticklabels(labels = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'], rotation = 'horizontal')
plt.show()

ord_hour = retail.groupby(by = ['invoice'])['hour'].unique().value_counts().sort_index()
ord_hour

oh = ord_hour.plot(kind='bar', figsize = (15, 6))
oh.set_xlabel('Hour of the Day')
oh.set_ylabel('Number of Orders')
oh.set_title('Orders per Hour of the Day')
oh.set_xticklabels(labels = [i for i in range (7, 21)], rotation = 'horizontal')
plt.show()

q_item = retail.groupby(by = ['desc'], as_index = False)['quantity'].sum()
q_item.head()

q_item.sort_values(by = 'quantity', ascending = False).head()
q_item.sort_values(by = 'quantity', ascending = False).tail()
ord_coun = retail.groupby(['country'])['invoice'].count().sort_values()
ord_coun.head()

ocoun = ord_coun.plot(kind='barh', figsize = (15, 6))
ocoun.set_xlabel('Number of Orders')
ocoun.set_ylabel('Country')
ocoun.set_title('Orders per Country')
plt.show()

del ord_coun['United Kingdom']
ocoun2 = ord_coun.plot(kind='barh', figsize = (15, 6))
ocoun2.set_xlabel('Number of Orders')
ocoun2.set_ylabel('Country')
ocoun2.set_title('Orders per Country')
plt.show()

coun_spent = retail.groupby('country')['spent'].sum().sort_values()

cs = coun_spent.plot(kind='barh', figsize = (15, 6))
cs.set_xlabel('Amount Spent')
cs.set_ylabel('Country')
cs.set_title('Amount Spent per Country')
plt.show()

del coun_spent['United Kingdom']
cs2 = coun_spent.plot(kind='barh', figsize = (15, 6))
cs2.set_xlabel('Amount Spent')
cs2.set_ylabel('Country')
cs2.set_title('Amount Spent per Country')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18590945620144655, 'fraction_numerical': 0.02035896062148406, 'mean_word_length': 6.394059405940594, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '266731', 'license': 'MIT', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1395, 'n_words': 300, 'path': 'Chapter08/Exercise8.03/Exercise8.03.ipynb', 'repo_name': 'ivalderrama/packt_data_analysis_workshop', 'size': '3733'}