|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 182, |
| 5 | + "execution_count": 31, |
6 | 6 | "id": "d11c21dd", |
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
9 | 9 | "source": [ |
| 10 | + "#importing packages\n", |
| 11 | + "\n", |
10 | 12 | "import pandas as pd\n", |
11 | 13 | "from datetime import datetime\n", |
12 | 14 | "import statistics" |
13 | 15 | ] |
14 | 16 | }, |
15 | 17 | { |
16 | 18 | "cell_type": "code", |
17 | | - "execution_count": 183, |
| 19 | + "execution_count": 32, |
18 | 20 | "id": "09da7f87", |
19 | 21 | "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 | + ], |
21 | 40 | "source": [ |
| 41 | + "#lambda function for parsing date\n", |
22 | 42 | "dateparse = lambda x: datetime.strptime(x, '%d/%m/%Y')\n", |
23 | 43 | "\n", |
24 | 44 | "#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)" |
26 | 48 | ] |
27 | 49 | }, |
28 | 50 | { |
29 | 51 | "cell_type": "code", |
30 | | - "execution_count": 184, |
| 52 | + "execution_count": 33, |
31 | 53 | "id": "fc6af654", |
32 | 54 | "metadata": {}, |
33 | 55 | "outputs": [], |
34 | 56 | "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))" |
36 | 60 | ] |
37 | 61 | }, |
38 | 62 | { |
39 | 63 | "cell_type": "code", |
40 | | - "execution_count": 185, |
41 | | - "id": "4b629866", |
| 64 | + "execution_count": 34, |
| 65 | + "id": "9c60b5d4", |
42 | 66 | "metadata": {}, |
43 | | - "outputs": [], |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "name": "stdout", |
| 70 | + "output_type": "stream", |
| 71 | + "text": [ |
| 72 | + "Total Sales: 266000\n" |
| 73 | + ] |
| 74 | + } |
| 75 | + ], |
44 | 76 | "source": [ |
45 | 77 | "#prints the total sales\n", |
46 | 78 | "\n", |
47 | | - "total_sales = df['sales'].sum()" |
| 79 | + "total_sales = df['Sales'].sum()\n", |
| 80 | + "\n", |
| 81 | + "print(\"Total Sales: {}\".format(total_sales))" |
48 | 82 | ] |
49 | 83 | }, |
50 | 84 | { |
51 | 85 | "cell_type": "code", |
52 | | - "execution_count": 186, |
53 | | - "id": "6040e7c6", |
| 86 | + "execution_count": 35, |
| 87 | + "id": "e4915fca", |
54 | 88 | "metadata": {}, |
55 | 89 | "outputs": [], |
56 | 90 | "source": [ |
|
62 | 96 | }, |
63 | 97 | { |
64 | 98 | "cell_type": "code", |
65 | | - "execution_count": 187, |
66 | | - "id": "abe0d7e4", |
| 99 | + "execution_count": 36, |
| 100 | + "id": "0e7b5df7", |
67 | 101 | "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 | + ], |
69 | 114 | "source": [ |
70 | | - "#groups the data by categories\n", |
| 115 | + "#groups the data by categories and adding the sales per category\n", |
71 | 116 | "\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)" |
73 | 120 | ] |
74 | 121 | }, |
75 | 122 | { |
76 | 123 | "cell_type": "code", |
77 | | - "execution_count": 188, |
78 | | - "id": "c7cb9175", |
| 124 | + "execution_count": 37, |
| 125 | + "id": "57fd66c5", |
79 | 126 | "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 | + ], |
81 | 140 | "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", |
84 | 143 | "\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" |
88 | 149 | ] |
89 | 150 | }, |
90 | 151 | { |
91 | 152 | "cell_type": "code", |
92 | | - "execution_count": 198, |
93 | | - "id": "bb54e96b", |
| 153 | + "execution_count": 38, |
| 154 | + "id": "15cd6df2", |
94 | 155 | "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 | + ], |
96 | 177 | "source": [ |
97 | | - "#groups the data by date (month) then categories\n", |
98 | | - "df_by_date = df.groupby(df['date'].dt.month)['sales'].sum()\n", |
99 | 178 | "\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", |
103 | 180 | "\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", |
105 | 182 | "\n", |
106 | | - "max = df.groupby([df['date'].dt.month])['sales'].max()\n", |
| 183 | + "min = df.groupby([df['Date'].dt.month])['Sales'].min()\n", |
107 | 184 | "\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", |
109 | 187 | "\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", |
110 | 194 | "# append output into the text file\n", |
111 | 195 | "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", |
115 | 204 | " ))\n", |
116 | 205 | "\n", |
117 | 206 | "# append output into the text file\n", |
118 | 207 | "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", |
122 | 216 | " ))\n", |
123 | 217 | " \n", |
124 | 218 | "# append output into the text file\n", |
125 | 219 | "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", |
129 | 227 | " ))" |
130 | 228 | ] |
131 | 229 | }, |
132 | 230 | { |
133 | 231 | "cell_type": "code", |
134 | | - "execution_count": 190, |
135 | | - "id": "9f6e631e", |
| 232 | + "execution_count": 39, |
| 233 | + "id": "3af042bc", |
136 | 234 | "metadata": {}, |
137 | 235 | "outputs": [ |
138 | 236 | { |
139 | 237 | "name": "stdout", |
140 | 238 | "output_type": "stream", |
141 | 239 | "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", |
142 | 280 | "Completed generating sales report!\n" |
143 | 281 | ] |
144 | 282 | } |
145 | 283 | ], |
146 | 284 | "source": [ |
147 | 285 | "# get product % for clothes\n", |
148 | 286 | "\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", |
151 | 289 | "\n", |
152 | | - "products_total = clothes + shoes\n", |
| 290 | + "products_total = rice + beans\n", |
153 | 291 | "\n", |
154 | 292 | "# append output into the text file\n", |
155 | 293 | "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", |
157 | 297 | " \n", |
158 | 298 | "print(\"Completed generating sales report!\")" |
159 | 299 | ] |
160 | 300 | }, |
161 | 301 | { |
162 | 302 | "cell_type": "code", |
163 | 303 | "execution_count": null, |
164 | | - "id": "6d4bede0", |
| 304 | + "id": "2ca6f625", |
165 | 305 | "metadata": {}, |
166 | 306 | "outputs": [], |
167 | 307 | "source": [] |
|
0 commit comments