Skip to content

Commit 4982228

Browse files
committed
feat: numpy data concatenation examples
1 parent 59b60a5 commit 4982228

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

notebooks/sn07_combining_datasets.ipynb

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,207 @@
347347
"# Filter out orders not present in `orders` list by checkitn\n",
348348
"sum(price * quantity for _, date, _, _, _, price, quantity in order_details_right if date != None)"
349349
]
350+
},
351+
{
352+
"cell_type": "markdown",
353+
"id": "c91aeb71-ec59-49f4-9610-ae13b635d5dc",
354+
"metadata": {},
355+
"source": [
356+
"## Concatenating NumPy Arrays"
357+
]
358+
},
359+
{
360+
"cell_type": "code",
361+
"execution_count": 2,
362+
"id": "6c7bf128-fdf3-4933-9e4f-4be84fcc021e",
363+
"metadata": {},
364+
"outputs": [
365+
{
366+
"data": {
367+
"text/plain": [
368+
"array([[2700, 3000, 3000],\n",
369+
" [2600, 2800, 2800],\n",
370+
" [2300, 2500, 2500]])"
371+
]
372+
},
373+
"metadata": {},
374+
"output_type": "display_data"
375+
}
376+
],
377+
"source": [
378+
"import numpy as np\n",
379+
"\n",
380+
"jeff_salary = [2700, 3000, 3000]\n",
381+
"nick_salary = [2600, 2800, 2800]\n",
382+
"tom_salary = [2300, 2500, 2500]\n",
383+
"\n",
384+
"base_salary1 = np.array([jeff_salary, nick_salary, tom_salary])\n",
385+
"display(base_salary1)"
386+
]
387+
},
388+
{
389+
"cell_type": "code",
390+
"execution_count": 3,
391+
"id": "b628098a-882a-44b2-a3af-0f892e8aabbd",
392+
"metadata": {},
393+
"outputs": [
394+
{
395+
"data": {
396+
"text/plain": [
397+
"array([[2200, 2400, 3000],\n",
398+
" [2500, 2700, 2700]])"
399+
]
400+
},
401+
"metadata": {},
402+
"output_type": "display_data"
403+
}
404+
],
405+
"source": [
406+
"maya_salary = [2200, 2400, 3000]\n",
407+
"john_salary = [2500, 2700, 2700]\n",
408+
"\n",
409+
"base_salary2 = np.array([maya_salary, john_salary])\n",
410+
"display(base_salary2)"
411+
]
412+
},
413+
{
414+
"cell_type": "code",
415+
"execution_count": 5,
416+
"id": "a4c46e08-f7d9-4574-af52-d4de13e70484",
417+
"metadata": {},
418+
"outputs": [
419+
{
420+
"data": {
421+
"text/plain": [
422+
"array([[2700, 3000, 3000],\n",
423+
" [2600, 2800, 2800],\n",
424+
" [2300, 2500, 2500],\n",
425+
" [2200, 2400, 3000],\n",
426+
" [2500, 2700, 2700]])"
427+
]
428+
},
429+
"metadata": {},
430+
"output_type": "display_data"
431+
}
432+
],
433+
"source": [
434+
"base_salary = np.concatenate((base_salary1, base_salary2), axis=0) # axis=0 concatenates vertically\n",
435+
"display(base_salary)"
436+
]
437+
},
438+
{
439+
"cell_type": "markdown",
440+
"id": "07b927fa-e087-4ac4-bd6b-904332666147",
441+
"metadata": {},
442+
"source": [
443+
"With both salary arrays merged into one, we can now open scenario for a case where a single month salary per employee is also introduced.\n",
444+
"In this case we want to concatenate an array of salaries that holds the same order as of employees."
445+
]
446+
},
447+
{
448+
"cell_type": "code",
449+
"execution_count": 6,
450+
"id": "c99a4a45-f87d-4d5a-b9cc-e242f97a505f",
451+
"metadata": {},
452+
"outputs": [],
453+
"source": [
454+
"# Introduces a new month salary for each employee where each item (array in the 2nd dimension) belongs to an employee\n",
455+
"new_month_salary = [\n",
456+
" [3000],\n",
457+
" [2900],\n",
458+
" [2500],\n",
459+
" [2500],\n",
460+
" [2700]]"
461+
]
462+
},
463+
{
464+
"cell_type": "code",
465+
"execution_count": 8,
466+
"id": "ed678c17-6ece-4b47-b31d-827232dff8c7",
467+
"metadata": {},
468+
"outputs": [
469+
{
470+
"data": {
471+
"text/plain": [
472+
"array([[2700, 3000, 3000, 3000],\n",
473+
" [2600, 2800, 2800, 2900],\n",
474+
" [2300, 2500, 2500, 2500],\n",
475+
" [2200, 2400, 3000, 2500],\n",
476+
" [2500, 2700, 2700, 2700]])"
477+
]
478+
},
479+
"metadata": {},
480+
"output_type": "display_data"
481+
}
482+
],
483+
"source": [
484+
"# Concatenates the `new_month_salary` to the `base_salary` array. Uses axis=1 to concatenate horizontally\n",
485+
"base_salary = np.concatenate((base_salary, new_month_salary), axis=1)\n",
486+
"display(base_salary)"
487+
]
488+
},
489+
{
490+
"cell_type": "code",
491+
"execution_count": 10,
492+
"id": "9ba4aee1-389f-494e-b25d-3278e6219ace",
493+
"metadata": {},
494+
"outputs": [
495+
{
496+
"data": {
497+
"text/plain": [
498+
"array([[2700, 3000, 3000, 3000, 3000, 3200],\n",
499+
" [2600, 2800, 2800, 2900, 2900, 2900],\n",
500+
" [2300, 2500, 2500, 2500, 2500, 2900],\n",
501+
" [2200, 2400, 3000, 2500, 3000, 3000],\n",
502+
" [2500, 2700, 2700, 2700, 2900, 2900]])"
503+
]
504+
},
505+
"metadata": {},
506+
"output_type": "display_data"
507+
}
508+
],
509+
"source": [
510+
"upcoming_months = [\n",
511+
" [3000, 3200],\n",
512+
" [2900, 2900],\n",
513+
" [2500, 2900],\n",
514+
" [3000, 3000],\n",
515+
" [2900, 2900],\n",
516+
"]\n",
517+
"\n",
518+
"base_salary = np.concatenate((base_salary, upcoming_months), axis=1)\n",
519+
"display(base_salary)"
520+
]
521+
},
522+
{
523+
"cell_type": "code",
524+
"execution_count": 13,
525+
"id": "4c73221b-625b-40cd-a305-32a3a4e3dda3",
526+
"metadata": {},
527+
"outputs": [
528+
{
529+
"data": {
530+
"text/plain": [
531+
"array([[2700, 3000, 3000, 3000, 3000, 3200],\n",
532+
" [2600, 2800, 2800, 2900, 2900, 2900],\n",
533+
" [2300, 2500, 2500, 2500, 2500, 2900],\n",
534+
" [2200, 2400, 3000, 2500, 3000, 3000],\n",
535+
" [2500, 2700, 2700, 2700, 2900, 2900],\n",
536+
" [ 0, 0, 0, 0, 0, 2900]])"
537+
]
538+
},
539+
"metadata": {},
540+
"output_type": "display_data"
541+
}
542+
],
543+
"source": [
544+
"new_employee_salary = [\n",
545+
" [0, 0, 0, 0, 0, 2900]\n",
546+
"]\n",
547+
"\n",
548+
"base_salary = np.concatenate((base_salary, new_employee_salary), axis=0)\n",
549+
"display(base_salary)"
550+
]
350551
}
351552
],
352553
"metadata": {

0 commit comments

Comments
 (0)