Skip to content

Commit 7785068

Browse files
committed
new dataset
1 parent 69457f8 commit 7785068

File tree

3 files changed

+209
-59
lines changed

3 files changed

+209
-59
lines changed

Sales_report_Jessy.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Date,Category,Sales
2+
01/08/2016,Rice,40000
3+
08/08/2016,Beans,15000
4+
20/08/2016,Rice,10000
5+
10/11/2016,Beans,60000
6+
19/11/2016,Rice,12000
7+
25/11/2016,Rice,1000
8+
30/12/2016,Beans,13000
9+
10/12/2016,Rice,15000
10+
12/12/2016,Beans,100000

monthly-sales-calculations.ipynb

Lines changed: 192 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,89 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 182,
5+
"execution_count": 31,
66
"id": "d11c21dd",
77
"metadata": {},
88
"outputs": [],
99
"source": [
10+
"#importing packages\n",
11+
"\n",
1012
"import pandas as pd\n",
1113
"from datetime import datetime\n",
1214
"import statistics"
1315
]
1416
},
1517
{
1618
"cell_type": "code",
17-
"execution_count": 183,
19+
"execution_count": 32,
1820
"id": "09da7f87",
1921
"metadata": {},
20-
"outputs": [],
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
" Date Category Sales\n",
28+
"0 2016-08-01 Rice 40000\n",
29+
"1 2016-08-08 Beans 15000\n",
30+
"2 2016-08-20 Rice 10000\n",
31+
"3 2016-11-10 Beans 60000\n",
32+
"4 2016-11-19 Rice 12000\n",
33+
"5 2016-11-25 Rice 1000\n",
34+
"6 2016-12-30 Beans 13000\n",
35+
"7 2016-12-10 Rice 15000\n",
36+
"8 2016-12-12 Beans 100000\n"
37+
]
38+
}
39+
],
2140
"source": [
41+
"#lambda function for parsing date\n",
2242
"dateparse = lambda x: datetime.strptime(x, '%d/%m/%Y')\n",
2343
"\n",
2444
"#importing csv file\n",
25-
"df = pd.read_csv('sales_data.csv', parse_dates=['date'], date_parser=dateparse)"
45+
"df = pd.read_csv('Sales_report_Jessy.csv', parse_dates=['Date'], date_parser=dateparse)\n",
46+
"\n",
47+
"print(df)"
2648
]
2749
},
2850
{
2951
"cell_type": "code",
30-
"execution_count": 184,
52+
"execution_count": 33,
3153
"id": "fc6af654",
3254
"metadata": {},
3355
"outputs": [],
3456
"source": [
35-
"dl = df.groupby(df['date'].map(lambda x: x.month))"
57+
"#testing grouping\n",
58+
"\n",
59+
"dl = df.groupby(df['Date'].map(lambda x: x.month))"
3660
]
3761
},
3862
{
3963
"cell_type": "code",
40-
"execution_count": 185,
41-
"id": "4b629866",
64+
"execution_count": 34,
65+
"id": "9c60b5d4",
4266
"metadata": {},
43-
"outputs": [],
67+
"outputs": [
68+
{
69+
"name": "stdout",
70+
"output_type": "stream",
71+
"text": [
72+
"Total Sales: 266000\n"
73+
]
74+
}
75+
],
4476
"source": [
4577
"#prints the total sales\n",
4678
"\n",
47-
"total_sales = df['sales'].sum()"
79+
"total_sales = df['Sales'].sum()\n",
80+
"\n",
81+
"print(\"Total Sales: {}\".format(total_sales))"
4882
]
4983
},
5084
{
5185
"cell_type": "code",
52-
"execution_count": 186,
53-
"id": "6040e7c6",
86+
"execution_count": 35,
87+
"id": "e4915fca",
5488
"metadata": {},
5589
"outputs": [],
5690
"source": [
@@ -62,106 +96,212 @@
6296
},
6397
{
6498
"cell_type": "code",
65-
"execution_count": 187,
66-
"id": "abe0d7e4",
99+
"execution_count": 36,
100+
"id": "0e7b5df7",
67101
"metadata": {},
68-
"outputs": [],
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"Category\n",
108+
"Beans 188000\n",
109+
"Rice 78000\n",
110+
"Name: Sales, dtype: int64\n"
111+
]
112+
}
113+
],
69114
"source": [
70-
"#groups the data by categories\n",
115+
"#groups the data by categories and adding the sales per category\n",
71116
"\n",
72-
"dl_categories = df.groupby('categories')['sales'].sum()\n"
117+
"dl_categories = df.groupby('Category')['Sales'].sum()\n",
118+
"\n",
119+
"print(dl_categories)"
73120
]
74121
},
75122
{
76123
"cell_type": "code",
77-
"execution_count": 188,
78-
"id": "c7cb9175",
124+
"execution_count": 37,
125+
"id": "57fd66c5",
79126
"metadata": {},
80-
"outputs": [],
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"Date\n",
133+
"8 65000\n",
134+
"11 73000\n",
135+
"12 128000\n",
136+
"Name: Sales, dtype: int64\n"
137+
]
138+
}
139+
],
81140
"source": [
82-
"# get average per month\n",
83-
"average_sales_per_month = [january_sales, february_sales, march_sales]\n",
141+
"#groups the data by date (month) then categories\n",
142+
"df_by_date = df.groupby(df['Date'].dt.month)['Sales'].sum()\n",
84143
"\n",
85-
"# append output into the text file\n",
86-
"with open(\"sales_report.txt\", \"a\") as f:\n",
87-
" f.write(\"Average Sales per Month: {} \\n \\n\".format(statistics.mean(average_sales_per_month)))"
144+
"print(df_by_date)\n",
145+
"\n",
146+
"august_sales = df_by_date[8]\n",
147+
"november_sales = df_by_date[11]\n",
148+
"december_sales = df_by_date[12]\n"
88149
]
89150
},
90151
{
91152
"cell_type": "code",
92-
"execution_count": 198,
93-
"id": "bb54e96b",
153+
"execution_count": 38,
154+
"id": "15cd6df2",
94155
"metadata": {},
95-
"outputs": [],
156+
"outputs": [
157+
{
158+
"name": "stdout",
159+
"output_type": "stream",
160+
"text": [
161+
"Category\n",
162+
"Beans 15000\n",
163+
"Rice 50000\n",
164+
"Name: Sales, dtype: int64\n",
165+
"August Sales: 65000 | Rice: 15000 | Beans: 50000 | Highest: 40000 | Lowest: 10000 \n",
166+
" \n",
167+
"\n",
168+
"November Sales: 73000 | Rice: 60000 | Beans: 13000 | Highest: 60000 | Lowest: 1000 \n",
169+
" \n",
170+
"\n",
171+
"December Sales: 128000 | Rice: 113000 | Beans: 15000 | Highest: 100000 | Lowest: 13000 \n",
172+
" \n",
173+
"\n"
174+
]
175+
}
176+
],
96177
"source": [
97-
"#groups the data by date (month) then categories\n",
98-
"df_by_date = df.groupby(df['date'].dt.month)['sales'].sum()\n",
99178
"\n",
100-
"january_sales = df_by_date[1]\n",
101-
"february_sales = df_by_date[2]\n",
102-
"march_sales = df_by_date[3]\n",
179+
"df_monthly_sales_by_category = df.groupby([df['Date'].dt.month, \"Category\"])['Sales'].sum()\n",
103180
"\n",
104-
"df_monthly_sales_by_category = df.groupby([df['date'].dt.month, \"categories\"])['sales'].sum()\n",
181+
"max = df.groupby([df['Date'].dt.month])['Sales'].max()\n",
105182
"\n",
106-
"max = df.groupby([df['date'].dt.month])['sales'].max()\n",
183+
"min = df.groupby([df['Date'].dt.month])['Sales'].min()\n",
107184
"\n",
108-
"min = df.groupby([df['date'].dt.month])['sales'].min()\n",
185+
"print(\"Maximum per month: {}\".format(max))\n",
186+
"print(\"Minimum per month: {}\".format(min))\n",
109187
"\n",
188+
"#handle exception\n",
189+
"try:\n",
190+
" print(df_monthly_sales_by_category[8])\n",
191+
"except(e):\n",
192+
" print(e)\n",
193+
" \n",
110194
"# append output into the text file\n",
111195
"with open(\"sales_report.txt\", \"a\") as f:\n",
112-
" f.write(\"January Sales: {} | Clothes: {} | Shoes: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
113-
" .format(january_sales, df_monthly_sales_by_category[1][0], df_monthly_sales_by_category[1][1],\n",
114-
" max[1], min[1]\n",
196+
" f.write(\"August Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
197+
" .format(august_sales, df_monthly_sales_by_category[8][0], df_monthly_sales_by_category[8][1],\n",
198+
" max[8], min[8]\n",
199+
" ))\n",
200+
" \n",
201+
"print(\"August Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
202+
" .format(august_sales, df_monthly_sales_by_category[8][0], df_monthly_sales_by_category[8][1],\n",
203+
" max[8], min[8]\n",
115204
" ))\n",
116205
"\n",
117206
"# append output into the text file\n",
118207
"with open(\"sales_report.txt\", \"a\") as f:\n",
119-
" f.write(\"February Sales: {} | Clothes: {} | Shoes: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
120-
" .format(february_sales, df_monthly_sales_by_category[2][0], df_monthly_sales_by_category[2][1],\n",
121-
" max[2], min[2]\n",
208+
" f.write(\"November Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
209+
" .format(november_sales, df_monthly_sales_by_category[11][0], df_monthly_sales_by_category[11][1],\n",
210+
" max[11], min[11]\n",
211+
" ))\n",
212+
"\n",
213+
"print(\"November Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
214+
" .format(november_sales, df_monthly_sales_by_category[11][0], df_monthly_sales_by_category[11][1],\n",
215+
" max[11], min[11]\n",
122216
" ))\n",
123217
" \n",
124218
"# append output into the text file\n",
125219
"with open(\"sales_report.txt\", \"a\") as f:\n",
126-
" f.write(\"March Sales: {} | Clothes: {} | Shoes: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
127-
" .format(march_sales, df_monthly_sales_by_category[3][0], df_monthly_sales_by_category[3][1],\n",
128-
" max[3], min[3]\n",
220+
" f.write(\"December Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
221+
" .format(december_sales, df_monthly_sales_by_category[12][0], df_monthly_sales_by_category[12][1],\n",
222+
" max[12], min[12]\n",
223+
" ))\n",
224+
"print(\"December Sales: {} | Rice: {} | Beans: {} | Highest: {} | Lowest: {} \\n \\n\"\n",
225+
" .format(december_sales, df_monthly_sales_by_category[12][0], df_monthly_sales_by_category[12][1],\n",
226+
" max[12], min[12]\n",
129227
" ))"
130228
]
131229
},
132230
{
133231
"cell_type": "code",
134-
"execution_count": 190,
135-
"id": "9f6e631e",
232+
"execution_count": 39,
233+
"id": "3af042bc",
136234
"metadata": {},
137235
"outputs": [
138236
{
139237
"name": "stdout",
140238
"output_type": "stream",
141239
"text": [
240+
"Average Sales per Month: 88666 \n",
241+
" \n",
242+
"\n"
243+
]
244+
}
245+
],
246+
"source": [
247+
"# get average per month\n",
248+
"average_sales_per_month = [august_sales, november_sales, december_sales]\n",
249+
"\n",
250+
"# append output into the text file\n",
251+
"with open(\"sales_report.txt\", \"a\") as f:\n",
252+
" f.write(\"Average Sales per Month: {} \\n \\n\".format(statistics.mean(average_sales_per_month)))\n",
253+
" \n",
254+
"print(\"Average Sales per Month: {} \\n \\n\".format(statistics.mean(average_sales_per_month)))"
255+
]
256+
},
257+
{
258+
"cell_type": "raw",
259+
"id": "dbf19e5d",
260+
"metadata": {},
261+
"source": []
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 40,
266+
"id": "8c8c9e0a",
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"Sales by Product Category: \n",
274+
" \n",
275+
" Rice: 188000 (70.67669172932331)% \n",
276+
" \n",
277+
" Beans: 78000 (29.32330827067669)% \n",
278+
" \n",
279+
"\n",
142280
"Completed generating sales report!\n"
143281
]
144282
}
145283
],
146284
"source": [
147285
"# get product % for clothes\n",
148286
"\n",
149-
"clothes = dl_categories[0]\n",
150-
"shoes = dl_categories[1]\n",
287+
"rice = dl_categories[0]\n",
288+
"beans = dl_categories[1]\n",
151289
"\n",
152-
"products_total = clothes + shoes\n",
290+
"products_total = rice + beans\n",
153291
"\n",
154292
"# append output into the text file\n",
155293
"with open(\"sales_report.txt\", \"a\") as f:\n",
156-
" f.write(\"Sales by Product Category: \\n \\n Clothes: {} ({})% \\n \\n Shoes: {} ({})% \\n \\n\".format(clothes, (clothes / products_total) * 100, shoes, (shoes / products_total) * 100))\n",
294+
" f.write(\"Sales by Product Category: \\n \\n Rice: {} ({})% \\n \\n Beans: {} ({})% \\n \\n\".format(rice, (rice / products_total) * 100, beans, (beans / products_total) * 100))\n",
295+
" \n",
296+
"print(\"Sales by Product Category: \\n \\n Rice: {} ({})% \\n \\n Beans: {} ({})% \\n \\n\".format(rice, (rice / products_total) * 100, beans, (beans / products_total) * 100))\n",
157297
" \n",
158298
"print(\"Completed generating sales report!\")"
159299
]
160300
},
161301
{
162302
"cell_type": "code",
163303
"execution_count": null,
164-
"id": "6d4bede0",
304+
"id": "2ca6f625",
165305
"metadata": {},
166306
"outputs": [],
167307
"source": []

sales_report.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
Total Sales: 990
1+
Total Sales: 266000
22

3-
Average Sales per Month: 330
3+
August Sales: 65000 | Rice: 15000 | Beans: 50000 | Highest: 40000 | Lowest: 10000
44

5-
January Sales: 490 | Clothes: 220 | Shoes: 270 | Highest: 140 | Lowest: 100
5+
November Sales: 73000 | Rice: 60000 | Beans: 13000 | Highest: 60000 | Lowest: 1000
66

7-
February Sales: 240 | Clothes: 150 | Shoes: 90 | Highest: 150 | Lowest: 90
7+
December Sales: 128000 | Rice: 113000 | Beans: 15000 | Highest: 100000 | Lowest: 13000
88

9-
March Sales: 260 | Clothes: 100 | Shoes: 160 | Highest: 160 | Lowest: 100
9+
Average Sales per Month: 88666
1010

1111
Sales by Product Category:
1212

13-
Clothes: 470 (47.474747474747474)%
13+
Rice: 188000 (70.67669172932331)%
1414

15-
Shoes: 520 (52.52525252525253)%
15+
Beans: 78000 (29.32330827067669)%
1616

0 commit comments

Comments
 (0)