Skip to content

Commit eb13281

Browse files
committed
Created using Colab
1 parent 3eec459 commit eb13281

File tree

1 file changed

+334
-5
lines changed

1 file changed

+334
-5
lines changed

MyPyApr0625.ipynb

+334-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"metadata": {
55
"colab": {
66
"provenance": [],
7-
"authorship_tag": "ABX9TyNwl8P3FqcCrTrrQzxhk/v+",
7+
"authorship_tag": "ABX9TyNO/kEgvMMcn4Tjl4wyiSko",
88
"include_colab_link": true
99
},
1010
"kernelspec": {
@@ -28,12 +28,341 @@
2828
},
2929
{
3030
"cell_type": "code",
31-
"execution_count": null,
31+
"execution_count": 2,
32+
"metadata": {
33+
"colab": {
34+
"base_uri": "https://localhost:8080/",
35+
"height": 122
36+
},
37+
"id": "T5hlPyVxu1mP",
38+
"outputId": "3828e00f-1fe8-44ca-9afa-7e6e1b5ccfcc"
39+
},
40+
"outputs": [
41+
{
42+
"output_type": "execute_result",
43+
"data": {
44+
"text/plain": [
45+
"'\\nProblem:\\n\\nYou are given a dataset of transactions from an e-commerce platform in the form of a list of dictionaries. \\nEach dictionary contains information about a single transaction. The dataset is as follows:\\n\\ntransactions = [\\n {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 10:00:00\"},\\n {\"order_id\": \"1002\", \"customer_id\": \"C002\", \"amount\": 200.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 11:00:00\"},\\n {\"order_id\": \"1003\", \"customer_id\": \"C001\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 12:00:00\"},\\n {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 13:00:00\"},\\n {\"order_id\": \"1005\", \"customer_id\": \"C001\", \"amount\": 75.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 14:00:00\"},\\n {\"order_id\": \"1006\", \"customer_id\": \"C002\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 15:00:00\"}\\n]\\n'"
46+
],
47+
"application/vnd.google.colaboratory.intrinsic+json": {
48+
"type": "string"
49+
}
50+
},
51+
"metadata": {},
52+
"execution_count": 2
53+
}
54+
],
55+
"source": [
56+
"'''\n",
57+
"Problem:\n",
58+
"\n",
59+
"You are given a dataset of transactions from an e-commerce platform in the form of a list of dictionaries.\n",
60+
"Each dictionary contains information about a single transaction. The dataset is as follows:\n",
61+
"\n",
62+
"transactions = [\n",
63+
" {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 10:00:00\"},\n",
64+
" {\"order_id\": \"1002\", \"customer_id\": \"C002\", \"amount\": 200.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 11:00:00\"},\n",
65+
" {\"order_id\": \"1003\", \"customer_id\": \"C001\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 12:00:00\"},\n",
66+
" {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 13:00:00\"},\n",
67+
" {\"order_id\": \"1005\", \"customer_id\": \"C001\", \"amount\": 75.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 14:00:00\"},\n",
68+
" {\"order_id\": \"1006\", \"customer_id\": \"C002\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 15:00:00\"}\n",
69+
"]\n",
70+
"\n",
71+
"Write a Python function to perform the following tasks:\n",
72+
"\n",
73+
"Filter and transform the dataset:\n",
74+
"\n",
75+
"Filter transactions to only include those that are from the Electronics category.\n",
76+
"\n",
77+
"Extract the order_id, customer_id, and amount fields from each transaction in the Electronics category and store them in a new list of dictionaries. The list should look like:\n",
78+
"\n",
79+
"python\n",
80+
"Copy\n",
81+
"[\n",
82+
" {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0},\n",
83+
" {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0}\n",
84+
"]\n",
85+
"Calculate total spending per customer:\n",
86+
"\n",
87+
"For each customer, calculate the total amount they have spent across all transactions. Return a dictionary where the keys are customer_ids and the values are the total amount spent. For example:\n",
88+
"\n",
89+
"python\n",
90+
"Copy\n",
91+
"{\"C001\": 375.0, \"C002\": 250.0, \"C003\": 120.0}\n",
92+
"Find customers with the highest spending:\n",
93+
"\n",
94+
"Using the results from the previous step, identify the customer(s) with the highest total spending.\n",
95+
"\n",
96+
"Date format manipulation:\n",
97+
"\n",
98+
"Convert the timestamp field of each transaction from the string format \"YYYY-MM-DD HH:MM:SS\" to the format \"DD-MM-YYYY HH:MM:SS\". For example, \"2025-04-01 10:00:00\" should become \"01-04-2025 10:00:00\".\n",
99+
"\n",
100+
"Count transactions per category:\n",
101+
"\n",
102+
"Count the number of transactions in each category (Electronics, Clothing, Books, etc.) and return a dictionary of category counts.\n",
103+
"\n",
104+
"Loop through transactions:\n",
105+
"\n",
106+
"Write a for loop to iterate over the dataset, and print the order_id and amount of each transaction in the Clothing category.\n",
107+
"\n",
108+
"String manipulation:\n",
109+
"\n",
110+
"For each customer_id, print the customer_id with all characters in uppercase.\n",
111+
"'''"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"source": [
117+
"#Filter and transform the dataset:\n",
118+
"\n",
119+
"\n",
120+
"transactions = [\n",
121+
" {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 10:00:00\"},\n",
122+
" {\"order_id\": \"1002\", \"customer_id\": \"C002\", \"amount\": 200.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 11:00:00\"},\n",
123+
" {\"order_id\": \"1003\", \"customer_id\": \"C001\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 12:00:00\"},\n",
124+
" {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 13:00:00\"},\n",
125+
" {\"order_id\": \"1005\", \"customer_id\": \"C001\", \"amount\": 75.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 14:00:00\"},\n",
126+
" {\"order_id\": \"1006\", \"customer_id\": \"C002\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 15:00:00\"}\n",
127+
"]\n",
128+
"\n",
129+
"d = {}\n",
130+
"\n",
131+
"for diction in transactions:\n",
132+
" for key, value in diction.items():\n",
133+
" if value == 'Electronics':\n",
134+
" print(diction)\n",
135+
" else:\n",
136+
" None\n",
137+
"\n",
138+
"print(d)\n",
139+
"'''\n",
140+
"for dict in transactions:\n",
141+
" for key,value in enumerate(dict):\n",
142+
" d[key] = value\n",
143+
"\n",
144+
"\n",
145+
"print(d)\n",
146+
"\n",
147+
"'''\n",
148+
"\n",
149+
"#this solution is incorrect. check next for proper solution."
150+
],
151+
"metadata": {
152+
"colab": {
153+
"base_uri": "https://localhost:8080/",
154+
"height": 88
155+
},
156+
"id": "EGEaactVvfkD",
157+
"outputId": "f61a83ba-e43b-47c9-f3c7-49e22f4c92b4"
158+
},
159+
"execution_count": 18,
160+
"outputs": [
161+
{
162+
"output_type": "stream",
163+
"name": "stdout",
164+
"text": [
165+
"{'order_id': '1001', 'customer_id': 'C001', 'amount': 150.0, 'category': 'Electronics', 'timestamp': '2025-04-01 10:00:00'}\n",
166+
"{'order_id': '1004', 'customer_id': 'C003', 'amount': 120.0, 'category': 'Electronics', 'timestamp': '2025-04-01 13:00:00'}\n",
167+
"{}\n"
168+
]
169+
},
170+
{
171+
"output_type": "execute_result",
172+
"data": {
173+
"text/plain": [
174+
"'\\nfor dict in transactions:\\n for key,value in enumerate(dict):\\n d[key] = value\\n \\n\\nprint(d)\\n\\n'"
175+
],
176+
"application/vnd.google.colaboratory.intrinsic+json": {
177+
"type": "string"
178+
}
179+
},
180+
"metadata": {},
181+
"execution_count": 18
182+
}
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"source": [
188+
"transactions = [\n",
189+
" {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 10:00:00\"},\n",
190+
" {\"order_id\": \"1002\", \"customer_id\": \"C002\", \"amount\": 200.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 11:00:00\"},\n",
191+
" {\"order_id\": \"1003\", \"customer_id\": \"C001\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 12:00:00\"},\n",
192+
" {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 13:00:00\"},\n",
193+
" {\"order_id\": \"1005\", \"customer_id\": \"C001\", \"amount\": 75.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 14:00:00\"},\n",
194+
" {\"order_id\": \"1006\", \"customer_id\": \"C002\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 15:00:00\"}\n",
195+
"]\n",
196+
"\n",
197+
"lst = [{\"order_id\" : diction[\"order_id\"],\n",
198+
" \"customer_id\" : diction[\"customer_id\"],\n",
199+
" \"amount\" : diction[\"amount\"]}\n",
200+
" for diction in transactions if diction[\"category\"] == 'Electronics']\n",
201+
"print(lst)"
202+
],
203+
"metadata": {
204+
"colab": {
205+
"base_uri": "https://localhost:8080/"
206+
},
207+
"id": "G4tRYWXsv7nm",
208+
"outputId": "c354232e-1c7e-4498-e111-0ed5439cbbeb"
209+
},
210+
"execution_count": 22,
211+
"outputs": [
212+
{
213+
"output_type": "stream",
214+
"name": "stdout",
215+
"text": [
216+
"[{'order_id': '1001', 'customer_id': 'C001', 'amount': 150.0}, {'order_id': '1004', 'customer_id': 'C003', 'amount': 120.0}]\n"
217+
]
218+
}
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"source": [
224+
"##Calculate total spending per customer:\n",
225+
"\n",
226+
"#For each customer, calculate the total amount they have spent across all transactions. Return a dictionary where the keys are customer_ids and the values are the total amount spent. For example:\n",
227+
"\n",
228+
"transactions = [\n",
229+
" {\"order_id\": \"1001\", \"customer_id\": \"C001\", \"amount\": 150.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 10:00:00\"},\n",
230+
" {\"order_id\": \"1002\", \"customer_id\": \"C002\", \"amount\": 200.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 11:00:00\"},\n",
231+
" {\"order_id\": \"1003\", \"customer_id\": \"C001\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 12:00:00\"},\n",
232+
" {\"order_id\": \"1004\", \"customer_id\": \"C003\", \"amount\": 120.0, \"category\": \"Electronics\", \"timestamp\": \"2025-04-01 13:00:00\"},\n",
233+
" {\"order_id\": \"1005\", \"customer_id\": \"C001\", \"amount\": 75.0, \"category\": \"Clothing\", \"timestamp\": \"2025-04-01 14:00:00\"},\n",
234+
" {\"order_id\": \"1006\", \"customer_id\": \"C002\", \"amount\": 50.0, \"category\": \"Books\", \"timestamp\": \"2025-04-01 15:00:00\"}\n",
235+
"]\n",
236+
"'''\n",
237+
"lst = [{\"customer_id\": d1[\"customer_id\"], \"amount\": d1[\"amount\"]} for d1 in transactions]\n",
238+
"print(lst)\n",
239+
"\n",
240+
"dout = {}\n",
241+
"for d2 in lst:\n",
242+
" for key, value in enumerate(d2):\n",
243+
" dout[key] = value\n",
244+
"\n",
245+
"print(dout)\n",
246+
"\n",
247+
"\n",
248+
"'''\n",
249+
"\n",
250+
"#Working solution:\n",
251+
"\n",
252+
"d1 = {}\n",
253+
"\n",
254+
"for transaction in transactions:\n",
255+
" customerid = transaction[\"customer_id\"]\n",
256+
" amount = transaction[\"amount\"]\n",
257+
"\n",
258+
" if customerid in d1:\n",
259+
" d1[customerid] += amount\n",
260+
" else:\n",
261+
" d1[customerid] = amount\n",
262+
"\n",
263+
"\n",
264+
"print(d1)\n"
265+
],
266+
"metadata": {
267+
"colab": {
268+
"base_uri": "https://localhost:8080/"
269+
},
270+
"id": "3wLil4fL1Al3",
271+
"outputId": "35f78b3b-8395-413f-ace8-e2f525cf28ac"
272+
},
273+
"execution_count": 49,
274+
"outputs": [
275+
{
276+
"output_type": "stream",
277+
"name": "stdout",
278+
"text": [
279+
"{'C001': 275.0, 'C002': 250.0, 'C003': 120.0}\n"
280+
]
281+
}
282+
]
283+
},
284+
{
285+
"cell_type": "code",
286+
"source": [
287+
"#Using the results from the previous step, identify the customer(s) with the highest total spending.\n",
288+
"\n",
289+
"d1 = {}\n",
290+
"\n",
291+
"for transaction in transactions:\n",
292+
" customerid = transaction[\"customer_id\"]\n",
293+
" amount = transaction[\"amount\"]\n",
294+
"\n",
295+
" if customerid in d1:\n",
296+
" d1[customerid] += amount\n",
297+
" else:\n",
298+
" d1[customerid] = amount\n",
299+
"\n",
300+
"\n",
301+
"print(d1)\n",
302+
"\n",
303+
"maxvalue = max(d1.values())\n",
304+
"print(maxvalue)\n",
305+
"\n",
306+
"lst = [key for key,value in d1.items() if value == maxvalue ]\n",
307+
"print(lst)\n"
308+
],
309+
"metadata": {
310+
"colab": {
311+
"base_uri": "https://localhost:8080/"
312+
},
313+
"id": "Lo09NOhp4Jcf",
314+
"outputId": "0c1472cd-bcc3-41ec-fa71-93ec16807035"
315+
},
316+
"execution_count": 65,
317+
"outputs": [
318+
{
319+
"output_type": "stream",
320+
"name": "stdout",
321+
"text": [
322+
"{'C001': 275.0, 'C002': 250.0, 'C003': 120.0}\n",
323+
"275.0\n",
324+
"['C001']\n"
325+
]
326+
}
327+
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"source": [
332+
"\n",
333+
"\n"
334+
],
32335
"metadata": {
33-
"id": "T5hlPyVxu1mP"
336+
"colab": {
337+
"base_uri": "https://localhost:8080/",
338+
"height": 176
339+
},
340+
"id": "NC5CVTUpDbmq",
341+
"outputId": "e0204491-51b9-4777-a099-6e845571092f"
34342
},
35-
"outputs": [],
36-
"source": []
343+
"execution_count": 82,
344+
"outputs": [
345+
{
346+
"output_type": "error",
347+
"ename": "TypeError",
348+
"evalue": "'str' object is not callable",
349+
"traceback": [
350+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
351+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
352+
"\u001b[0;32m<ipython-input-82-b04f8843d3c4>\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter a STring \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
353+
"\u001b[0;31mTypeError\u001b[0m: 'str' object is not callable"
354+
]
355+
}
356+
]
357+
},
358+
{
359+
"cell_type": "code",
360+
"source": [],
361+
"metadata": {
362+
"id": "SEI1mSN4ED9L"
363+
},
364+
"execution_count": null,
365+
"outputs": []
37366
}
38367
]
39368
}

0 commit comments

Comments
 (0)