├── 10_pandas_DataFrame.ipynb
├── 1_hello_world.py
├── 2_variables.py
├── 3_user_input.py
├── 4_arithmetic_operators.py
├── 5_conditional_statements.py
├── 6_string_info.py
├── 7_string methods_1
├── 8_string_methods2.py
├── 9_pandas_series.ipynb
└── README.md
/10_pandas_DataFrame.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "7699f391",
6 | "metadata": {},
7 | "source": [
8 | "# DataFrames\n",
9 | "- 2 dimensional data structure\n",
10 | "- table with multiple rows and columns"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 142,
16 | "id": "6265dc96",
17 | "metadata": {},
18 | "outputs": [],
19 | "source": [
20 | "import pandas as pd"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "id": "a5a4c0aa",
26 | "metadata": {},
27 | "source": [
28 | "# constructing dataframe from dictionary"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 4,
34 | "id": "45114ac3",
35 | "metadata": {},
36 | "outputs": [
37 | {
38 | "data": {
39 | "text/html": [
40 | "
\n",
41 | "\n",
54 | "
\n",
55 | " \n",
56 | " \n",
57 | " | \n",
58 | " name | \n",
59 | " age | \n",
60 | " marks | \n",
61 | "
\n",
62 | " \n",
63 | " \n",
64 | " \n",
65 | " 0 | \n",
66 | " Ram | \n",
67 | " 22 | \n",
68 | " 79 | \n",
69 | "
\n",
70 | " \n",
71 | " 1 | \n",
72 | " Shyam | \n",
73 | " 23 | \n",
74 | " 91 | \n",
75 | "
\n",
76 | " \n",
77 | " 2 | \n",
78 | " Hari | \n",
79 | " 24 | \n",
80 | " 81 | \n",
81 | "
\n",
82 | " \n",
83 | " 3 | \n",
84 | " Sita | \n",
85 | " 21 | \n",
86 | " 90 | \n",
87 | "
\n",
88 | " \n",
89 | "
\n",
90 | "
"
91 | ],
92 | "text/plain": [
93 | " name age marks\n",
94 | "0 Ram 22 79\n",
95 | "1 Shyam 23 91\n",
96 | "2 Hari 24 81\n",
97 | "3 Sita 21 90"
98 | ]
99 | },
100 | "execution_count": 4,
101 | "metadata": {},
102 | "output_type": "execute_result"
103 | }
104 | ],
105 | "source": [
106 | "data1 = {\n",
107 | " 'name':['Ram','Shyam','Hari','Sita'],\n",
108 | " 'age':[22,23,24,21],\n",
109 | " 'marks':[79,91,81,90]\n",
110 | "}\n",
111 | "df = pd.DataFrame(data1)\n",
112 | "df"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 5,
118 | "id": "7193dd73",
119 | "metadata": {},
120 | "outputs": [
121 | {
122 | "data": {
123 | "text/plain": [
124 | "name object\n",
125 | "age int64\n",
126 | "marks int64\n",
127 | "dtype: object"
128 | ]
129 | },
130 | "execution_count": 5,
131 | "metadata": {},
132 | "output_type": "execute_result"
133 | }
134 | ],
135 | "source": [
136 | "df.dtypes"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": 144,
142 | "id": "cb918124",
143 | "metadata": {},
144 | "outputs": [
145 | {
146 | "data": {
147 | "text/html": [
148 | "\n",
149 | "\n",
162 | "
\n",
163 | " \n",
164 | " \n",
165 | " | \n",
166 | " age | \n",
167 | " marks | \n",
168 | "
\n",
169 | " \n",
170 | " \n",
171 | " \n",
172 | " 0 | \n",
173 | " 22 | \n",
174 | " 79 | \n",
175 | "
\n",
176 | " \n",
177 | " 1 | \n",
178 | " 23 | \n",
179 | " 91 | \n",
180 | "
\n",
181 | " \n",
182 | " 2 | \n",
183 | " 24 | \n",
184 | " 81 | \n",
185 | "
\n",
186 | " \n",
187 | " 3 | \n",
188 | " 21 | \n",
189 | " 90 | \n",
190 | "
\n",
191 | " \n",
192 | "
\n",
193 | "
"
194 | ],
195 | "text/plain": [
196 | " age marks\n",
197 | "0 22 79\n",
198 | "1 23 91\n",
199 | "2 24 81\n",
200 | "3 21 90"
201 | ]
202 | },
203 | "execution_count": 144,
204 | "metadata": {},
205 | "output_type": "execute_result"
206 | }
207 | ],
208 | "source": [
209 | "# Note: inferred data type is int64\n",
210 | "# this can be changed as well\n",
211 | "import numpy as np\n",
212 | "data1 = {\n",
213 | " 'age':[22,23,24,21],\n",
214 | " 'marks':[79,91,81,90]\n",
215 | "}\n",
216 | "df = pd.DataFrame(data1,dtype = np.int8)\n",
217 | "df"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 26,
223 | "id": "da42fbf0",
224 | "metadata": {},
225 | "outputs": [
226 | {
227 | "data": {
228 | "text/plain": [
229 | "age int8\n",
230 | "marks int8\n",
231 | "dtype: object"
232 | ]
233 | },
234 | "execution_count": 26,
235 | "metadata": {},
236 | "output_type": "execute_result"
237 | }
238 | ],
239 | "source": [
240 | "df.dtypes"
241 | ]
242 | },
243 | {
244 | "cell_type": "markdown",
245 | "id": "41bc0dc5",
246 | "metadata": {},
247 | "source": [
248 | "# constructing dataframe from ndarray"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 145,
254 | "id": "720f3e39",
255 | "metadata": {},
256 | "outputs": [
257 | {
258 | "data": {
259 | "text/html": [
260 | "\n",
261 | "\n",
274 | "
\n",
275 | " \n",
276 | " \n",
277 | " | \n",
278 | " 0 | \n",
279 | " 1 | \n",
280 | " 2 | \n",
281 | "
\n",
282 | " \n",
283 | " \n",
284 | " \n",
285 | " 0 | \n",
286 | " 1 | \n",
287 | " 2 | \n",
288 | " 3 | \n",
289 | "
\n",
290 | " \n",
291 | " 1 | \n",
292 | " 4 | \n",
293 | " 5 | \n",
294 | " 6 | \n",
295 | "
\n",
296 | " \n",
297 | " 2 | \n",
298 | " 7 | \n",
299 | " 8 | \n",
300 | " 9 | \n",
301 | "
\n",
302 | " \n",
303 | "
\n",
304 | "
"
305 | ],
306 | "text/plain": [
307 | " 0 1 2\n",
308 | "0 1 2 3\n",
309 | "1 4 5 6\n",
310 | "2 7 8 9"
311 | ]
312 | },
313 | "execution_count": 145,
314 | "metadata": {},
315 | "output_type": "execute_result"
316 | }
317 | ],
318 | "source": [
319 | "mydata = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
320 | "df = pd.DataFrame(mydata)\n",
321 | "df"
322 | ]
323 | },
324 | {
325 | "cell_type": "markdown",
326 | "id": "712c0209",
327 | "metadata": {},
328 | "source": [
329 | "- provide the name for columns\n",
330 | "- the above output might appear confusing. So, it will be effective to provide\n",
331 | "- the name for columns\n",
332 | "- it can be done by using the parameter columns"
333 | ]
334 | },
335 | {
336 | "cell_type": "code",
337 | "execution_count": 146,
338 | "id": "ce717f15",
339 | "metadata": {},
340 | "outputs": [
341 | {
342 | "data": {
343 | "text/html": [
344 | "\n",
345 | "\n",
358 | "
\n",
359 | " \n",
360 | " \n",
361 | " | \n",
362 | " detail1 | \n",
363 | " detail2 | \n",
364 | " detail3 | \n",
365 | "
\n",
366 | " \n",
367 | " \n",
368 | " \n",
369 | " a | \n",
370 | " 1 | \n",
371 | " 2 | \n",
372 | " 3 | \n",
373 | "
\n",
374 | " \n",
375 | " b | \n",
376 | " 4 | \n",
377 | " 5 | \n",
378 | " 6 | \n",
379 | "
\n",
380 | " \n",
381 | " c | \n",
382 | " 7 | \n",
383 | " 8 | \n",
384 | " 9 | \n",
385 | "
\n",
386 | " \n",
387 | "
\n",
388 | "
"
389 | ],
390 | "text/plain": [
391 | " detail1 detail2 detail3\n",
392 | "a 1 2 3\n",
393 | "b 4 5 6\n",
394 | "c 7 8 9"
395 | ]
396 | },
397 | "execution_count": 146,
398 | "metadata": {},
399 | "output_type": "execute_result"
400 | }
401 | ],
402 | "source": [
403 | "mydata = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
404 | "df = pd.DataFrame(mydata, columns=['detail1','detail2','detail3'],\n",
405 | " index=['a','b','c'])\n",
406 | "df"
407 | ]
408 | },
409 | {
410 | "cell_type": "markdown",
411 | "id": "aea972be",
412 | "metadata": {},
413 | "source": [
414 | "- Let's try to change the name of the indexes as well\n",
415 | "- this can be done using the paramter index"
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "execution_count": 147,
421 | "id": "4ccf8330",
422 | "metadata": {},
423 | "outputs": [
424 | {
425 | "data": {
426 | "text/html": [
427 | "\n",
428 | "\n",
441 | "
\n",
442 | " \n",
443 | " \n",
444 | " | \n",
445 | " ktm | \n",
446 | " brt | \n",
447 | " drn | \n",
448 | "
\n",
449 | " \n",
450 | " \n",
451 | " \n",
452 | " Sunday | \n",
453 | " 1 | \n",
454 | " 2 | \n",
455 | " 3 | \n",
456 | "
\n",
457 | " \n",
458 | " Monday | \n",
459 | " 4 | \n",
460 | " 5 | \n",
461 | " 6 | \n",
462 | "
\n",
463 | " \n",
464 | " Tuesday | \n",
465 | " 7 | \n",
466 | " 8 | \n",
467 | " 9 | \n",
468 | "
\n",
469 | " \n",
470 | "
\n",
471 | "
"
472 | ],
473 | "text/plain": [
474 | " ktm brt drn\n",
475 | "Sunday 1 2 3\n",
476 | "Monday 4 5 6\n",
477 | "Tuesday 7 8 9"
478 | ]
479 | },
480 | "execution_count": 147,
481 | "metadata": {},
482 | "output_type": "execute_result"
483 | }
484 | ],
485 | "source": [
486 | "mydata = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
487 | "df = pd.DataFrame(mydata, columns=['ktm','brt','drn'],\n",
488 | " index=['Sunday','Monday','Tuesday'])\n",
489 | "df"
490 | ]
491 | },
492 | {
493 | "cell_type": "markdown",
494 | "id": "ee370bc8",
495 | "metadata": {},
496 | "source": [
497 | "# access the value of ktm only"
498 | ]
499 | },
500 | {
501 | "cell_type": "markdown",
502 | "id": "767254e1",
503 | "metadata": {},
504 | "source": [
505 | "- method 1\n",
506 | "- for accessing the value we will use the name of dataframe which is df in our\n",
507 | "- case followed by the name of the column using dot operator(.)\n"
508 | ]
509 | },
510 | {
511 | "cell_type": "code",
512 | "execution_count": 149,
513 | "id": "f243ddd2",
514 | "metadata": {},
515 | "outputs": [
516 | {
517 | "data": {
518 | "text/plain": [
519 | "Sunday 1\n",
520 | "Monday 4\n",
521 | "Tuesday 7\n",
522 | "Name: ktm, dtype: int32"
523 | ]
524 | },
525 | "execution_count": 149,
526 | "metadata": {},
527 | "output_type": "execute_result"
528 | }
529 | ],
530 | "source": [
531 | "df.ktm"
532 | ]
533 | },
534 | {
535 | "cell_type": "markdown",
536 | "id": "736a1fae",
537 | "metadata": {},
538 | "source": [
539 | "- method 2 \n",
540 | "- we can access the data of particular column this way as well:\n"
541 | ]
542 | },
543 | {
544 | "cell_type": "code",
545 | "execution_count": 150,
546 | "id": "bf1a3bfb",
547 | "metadata": {},
548 | "outputs": [
549 | {
550 | "data": {
551 | "text/plain": [
552 | "Sunday 1\n",
553 | "Monday 4\n",
554 | "Tuesday 7\n",
555 | "Name: ktm, dtype: int32"
556 | ]
557 | },
558 | "execution_count": 150,
559 | "metadata": {},
560 | "output_type": "execute_result"
561 | }
562 | ],
563 | "source": [
564 | "df['ktm']"
565 | ]
566 | },
567 | {
568 | "cell_type": "markdown",
569 | "id": "2c672b7d",
570 | "metadata": {},
571 | "source": [
572 | "# loc and iloc"
573 | ]
574 | },
575 | {
576 | "cell_type": "code",
577 | "execution_count": 151,
578 | "id": "0505aab9",
579 | "metadata": {
580 | "scrolled": true
581 | },
582 | "outputs": [
583 | {
584 | "data": {
585 | "text/plain": [
586 | "ktm 1\n",
587 | "brt 2\n",
588 | "drn 3\n",
589 | "Name: Sunday, dtype: int32"
590 | ]
591 | },
592 | "execution_count": 151,
593 | "metadata": {},
594 | "output_type": "execute_result"
595 | }
596 | ],
597 | "source": [
598 | "# display the value of sunday \n",
599 | "# loc could be used\n",
600 | "# Access a group of rows and columns by label(s) or a boolean array.\n",
601 | "df.loc['Sunday']"
602 | ]
603 | },
604 | {
605 | "cell_type": "code",
606 | "execution_count": 152,
607 | "id": "1d7d6977",
608 | "metadata": {},
609 | "outputs": [
610 | {
611 | "data": {
612 | "text/html": [
613 | "\n",
614 | "\n",
627 | "
\n",
628 | " \n",
629 | " \n",
630 | " | \n",
631 | " ktm | \n",
632 | " brt | \n",
633 | " drn | \n",
634 | "
\n",
635 | " \n",
636 | " \n",
637 | " \n",
638 | " Sunday | \n",
639 | " 1 | \n",
640 | " 2 | \n",
641 | " 3 | \n",
642 | "
\n",
643 | " \n",
644 | " Monday | \n",
645 | " 4 | \n",
646 | " 5 | \n",
647 | " 6 | \n",
648 | "
\n",
649 | " \n",
650 | "
\n",
651 | "
"
652 | ],
653 | "text/plain": [
654 | " ktm brt drn\n",
655 | "Sunday 1 2 3\n",
656 | "Monday 4 5 6"
657 | ]
658 | },
659 | "execution_count": 152,
660 | "metadata": {},
661 | "output_type": "execute_result"
662 | }
663 | ],
664 | "source": [
665 | "# access the value of Sunday and Monday\n",
666 | "df.loc[['Sunday','Monday']]\n",
667 | "# Note: \n",
668 | "# using [[]] returns a DataFrame"
669 | ]
670 | },
671 | {
672 | "cell_type": "code",
673 | "execution_count": 153,
674 | "id": "28473d96",
675 | "metadata": {},
676 | "outputs": [
677 | {
678 | "data": {
679 | "text/plain": [
680 | "2"
681 | ]
682 | },
683 | "execution_count": 153,
684 | "metadata": {},
685 | "output_type": "execute_result"
686 | }
687 | ],
688 | "source": [
689 | "# access the detail of sunday for brt \n",
690 | "df.loc['Sunday']['brt']"
691 | ]
692 | },
693 | {
694 | "cell_type": "code",
695 | "execution_count": 87,
696 | "id": "1839d8e9",
697 | "metadata": {},
698 | "outputs": [
699 | {
700 | "data": {
701 | "text/plain": [
702 | "brt 2\n",
703 | "drn 3\n",
704 | "Name: Sunday, dtype: int32"
705 | ]
706 | },
707 | "execution_count": 87,
708 | "metadata": {},
709 | "output_type": "execute_result"
710 | }
711 | ],
712 | "source": [
713 | "# access the detail of sunday for brt and drn only\n",
714 | "df.loc['Sunday'][['brt','drn']]"
715 | ]
716 | },
717 | {
718 | "cell_type": "code",
719 | "execution_count": 154,
720 | "id": "c0803789",
721 | "metadata": {},
722 | "outputs": [
723 | {
724 | "data": {
725 | "text/html": [
726 | "\n",
727 | "\n",
740 | "
\n",
741 | " \n",
742 | " \n",
743 | " | \n",
744 | " brt | \n",
745 | " drn | \n",
746 | "
\n",
747 | " \n",
748 | " \n",
749 | " \n",
750 | " Sunday | \n",
751 | " 2 | \n",
752 | " 3 | \n",
753 | "
\n",
754 | " \n",
755 | " Monday | \n",
756 | " 5 | \n",
757 | " 6 | \n",
758 | "
\n",
759 | " \n",
760 | "
\n",
761 | "
"
762 | ],
763 | "text/plain": [
764 | " brt drn\n",
765 | "Sunday 2 3\n",
766 | "Monday 5 6"
767 | ]
768 | },
769 | "execution_count": 154,
770 | "metadata": {},
771 | "output_type": "execute_result"
772 | }
773 | ],
774 | "source": [
775 | "# access the detail of Sunday and Monday for brt and drn only\n",
776 | "df.loc[['Sunday','Monday']][['brt','drn']]"
777 | ]
778 | },
779 | {
780 | "cell_type": "markdown",
781 | "id": "cdc7614c",
782 | "metadata": {},
783 | "source": [
784 | "# Constructing DataFrame from Series"
785 | ]
786 | },
787 | {
788 | "cell_type": "code",
789 | "execution_count": 155,
790 | "id": "f725ea2a",
791 | "metadata": {},
792 | "outputs": [
793 | {
794 | "data": {
795 | "text/html": [
796 | "\n",
797 | "\n",
810 | "
\n",
811 | " \n",
812 | " \n",
813 | " | \n",
814 | " 0 | \n",
815 | "
\n",
816 | " \n",
817 | " \n",
818 | " \n",
819 | " Sun | \n",
820 | " 1 | \n",
821 | "
\n",
822 | " \n",
823 | " Mon | \n",
824 | " 2 | \n",
825 | "
\n",
826 | " \n",
827 | " Tue | \n",
828 | " 3 | \n",
829 | "
\n",
830 | " \n",
831 | " Wed | \n",
832 | " 4 | \n",
833 | "
\n",
834 | " \n",
835 | "
\n",
836 | "
"
837 | ],
838 | "text/plain": [
839 | " 0\n",
840 | "Sun 1\n",
841 | "Mon 2\n",
842 | "Tue 3\n",
843 | "Wed 4"
844 | ]
845 | },
846 | "execution_count": 155,
847 | "metadata": {},
848 | "output_type": "execute_result"
849 | }
850 | ],
851 | "source": [
852 | "my_series = pd.Series([1,2,3,4], index = ['Sun','Mon','Tue','Wed'])\n",
853 | "df = pd.DataFrame(my_series)\n",
854 | "df"
855 | ]
856 | },
857 | {
858 | "cell_type": "code",
859 | "execution_count": 156,
860 | "id": "0bf00061",
861 | "metadata": {},
862 | "outputs": [
863 | {
864 | "data": {
865 | "text/html": [
866 | "\n",
867 | "\n",
880 | "
\n",
881 | " \n",
882 | " \n",
883 | " | \n",
884 | " kathmandu | \n",
885 | "
\n",
886 | " \n",
887 | " \n",
888 | " \n",
889 | " Sun | \n",
890 | " 1 | \n",
891 | "
\n",
892 | " \n",
893 | " Mon | \n",
894 | " 2 | \n",
895 | "
\n",
896 | " \n",
897 | " Tue | \n",
898 | " 3 | \n",
899 | "
\n",
900 | " \n",
901 | " Wed | \n",
902 | " 4 | \n",
903 | "
\n",
904 | " \n",
905 | "
\n",
906 | "
"
907 | ],
908 | "text/plain": [
909 | " kathmandu\n",
910 | "Sun 1\n",
911 | "Mon 2\n",
912 | "Tue 3\n",
913 | "Wed 4"
914 | ]
915 | },
916 | "execution_count": 156,
917 | "metadata": {},
918 | "output_type": "execute_result"
919 | }
920 | ],
921 | "source": [
922 | "# let's name the column something\n",
923 | "my_series = pd.Series([1,2,3,4], index = ['Sun','Mon','Tue','Wed'])\n",
924 | "df = pd.DataFrame(my_series, columns=['kathmandu'])\n",
925 | "df"
926 | ]
927 | },
928 | {
929 | "cell_type": "markdown",
930 | "id": "70edd488",
931 | "metadata": {},
932 | "source": [
933 | "- let's check loc and iloc"
934 | ]
935 | },
936 | {
937 | "cell_type": "code",
938 | "execution_count": 157,
939 | "id": "2b120cf4",
940 | "metadata": {},
941 | "outputs": [
942 | {
943 | "data": {
944 | "text/html": [
945 | "\n",
946 | "\n",
959 | "
\n",
960 | " \n",
961 | " \n",
962 | " | \n",
963 | " ktm | \n",
964 | " brt | \n",
965 | " drn | \n",
966 | "
\n",
967 | " \n",
968 | " \n",
969 | " \n",
970 | " Sunday | \n",
971 | " 1 | \n",
972 | " 2 | \n",
973 | " 3 | \n",
974 | "
\n",
975 | " \n",
976 | " Monday | \n",
977 | " 4 | \n",
978 | " 5 | \n",
979 | " 6 | \n",
980 | "
\n",
981 | " \n",
982 | " Tuesday | \n",
983 | " 7 | \n",
984 | " 8 | \n",
985 | " 9 | \n",
986 | "
\n",
987 | " \n",
988 | "
\n",
989 | "
"
990 | ],
991 | "text/plain": [
992 | " ktm brt drn\n",
993 | "Sunday 1 2 3\n",
994 | "Monday 4 5 6\n",
995 | "Tuesday 7 8 9"
996 | ]
997 | },
998 | "execution_count": 157,
999 | "metadata": {},
1000 | "output_type": "execute_result"
1001 | }
1002 | ],
1003 | "source": [
1004 | "# construct one sample dataframe \n",
1005 | "mydata = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
1006 | "df = pd.DataFrame(mydata, columns=['ktm','brt','drn'],\n",
1007 | " index=['Sunday','Monday','Tuesday'])\n",
1008 | "df"
1009 | ]
1010 | },
1011 | {
1012 | "cell_type": "code",
1013 | "execution_count": 158,
1014 | "id": "75073142",
1015 | "metadata": {},
1016 | "outputs": [
1017 | {
1018 | "data": {
1019 | "text/plain": [
1020 | "ktm 1\n",
1021 | "brt 2\n",
1022 | "drn 3\n",
1023 | "Name: Sunday, dtype: int32"
1024 | ]
1025 | },
1026 | "execution_count": 158,
1027 | "metadata": {},
1028 | "output_type": "execute_result"
1029 | }
1030 | ],
1031 | "source": [
1032 | "# loc access the detail of row Sunday without any issue\n",
1033 | "df.loc['Sunday']"
1034 | ]
1035 | },
1036 | {
1037 | "cell_type": "code",
1038 | "execution_count": 159,
1039 | "id": "be570f9c",
1040 | "metadata": {},
1041 | "outputs": [
1042 | {
1043 | "ename": "TypeError",
1044 | "evalue": "Cannot index by location index with a non-integer key",
1045 | "output_type": "error",
1046 | "traceback": [
1047 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
1048 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
1049 | "Cell \u001b[1;32mIn[159], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df\u001b[38;5;241m.\u001b[39miloc[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSunday\u001b[39m\u001b[38;5;124m'\u001b[39m]\n",
1050 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexing.py:1192\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1190\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[0;32m 1191\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_deprecated_callable_usage(key, maybe_callable)\n\u001b[1;32m-> 1192\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_axis(maybe_callable, axis\u001b[38;5;241m=\u001b[39maxis)\n",
1051 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexing.py:1750\u001b[0m, in \u001b[0;36m_iLocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1748\u001b[0m key \u001b[38;5;241m=\u001b[39m item_from_zerodim(key)\n\u001b[0;32m 1749\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_integer(key):\n\u001b[1;32m-> 1750\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot index by location index with a non-integer key\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 1752\u001b[0m \u001b[38;5;66;03m# validate the location\u001b[39;00m\n\u001b[0;32m 1753\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_integer(key, axis)\n",
1052 | "\u001b[1;31mTypeError\u001b[0m: Cannot index by location index with a non-integer key"
1053 | ]
1054 | }
1055 | ],
1056 | "source": [
1057 | "df.iloc['Sunday']"
1058 | ]
1059 | },
1060 | {
1061 | "cell_type": "markdown",
1062 | "id": "52f5317f",
1063 | "metadata": {},
1064 | "source": [
1065 | "- TypeError: Cannot index by location index with a non-integer key is displayed\n",
1066 | "- implying that for accessing integer key iloc is used\n",
1067 | "\n",
1068 | " "
1069 | ]
1070 | },
1071 | {
1072 | "cell_type": "code",
1073 | "execution_count": 161,
1074 | "id": "520f5be4",
1075 | "metadata": {},
1076 | "outputs": [
1077 | {
1078 | "data": {
1079 | "text/html": [
1080 | "\n",
1081 | "\n",
1094 | "
\n",
1095 | " \n",
1096 | " \n",
1097 | " | \n",
1098 | " ktm | \n",
1099 | " brt | \n",
1100 | " drn | \n",
1101 | "
\n",
1102 | " \n",
1103 | " \n",
1104 | " \n",
1105 | " 1 | \n",
1106 | " 1 | \n",
1107 | " 2 | \n",
1108 | " 3 | \n",
1109 | "
\n",
1110 | " \n",
1111 | " 2 | \n",
1112 | " 4 | \n",
1113 | " 5 | \n",
1114 | " 6 | \n",
1115 | "
\n",
1116 | " \n",
1117 | " 3 | \n",
1118 | " 7 | \n",
1119 | " 8 | \n",
1120 | " 9 | \n",
1121 | "
\n",
1122 | " \n",
1123 | "
\n",
1124 | "
"
1125 | ],
1126 | "text/plain": [
1127 | " ktm brt drn\n",
1128 | "1 1 2 3\n",
1129 | "2 4 5 6\n",
1130 | "3 7 8 9"
1131 | ]
1132 | },
1133 | "execution_count": 161,
1134 | "metadata": {},
1135 | "output_type": "execute_result"
1136 | }
1137 | ],
1138 | "source": [
1139 | "# construct one sample dataframe with index as integers\n",
1140 | "mydata = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
1141 | "df = pd.DataFrame(mydata, columns=['ktm','brt','drn'],\n",
1142 | " index=[1,2,3])\n",
1143 | "df"
1144 | ]
1145 | },
1146 | {
1147 | "cell_type": "code",
1148 | "execution_count": 162,
1149 | "id": "b5a97598",
1150 | "metadata": {
1151 | "scrolled": true
1152 | },
1153 | "outputs": [
1154 | {
1155 | "data": {
1156 | "text/plain": [
1157 | "ktm 4\n",
1158 | "brt 5\n",
1159 | "drn 6\n",
1160 | "Name: 2, dtype: int32"
1161 | ]
1162 | },
1163 | "execution_count": 162,
1164 | "metadata": {},
1165 | "output_type": "execute_result"
1166 | }
1167 | ],
1168 | "source": [
1169 | "df.iloc[1]"
1170 | ]
1171 | },
1172 | {
1173 | "cell_type": "markdown",
1174 | "id": "04c6bbd8",
1175 | "metadata": {},
1176 | "source": [
1177 | "# Purely integer-location based indexing for selection by position."
1178 | ]
1179 | },
1180 | {
1181 | "cell_type": "code",
1182 | "execution_count": 129,
1183 | "id": "05ae7488",
1184 | "metadata": {},
1185 | "outputs": [
1186 | {
1187 | "data": {
1188 | "text/html": [
1189 | "\n",
1190 | "\n",
1203 | "
\n",
1204 | " \n",
1205 | " \n",
1206 | " | \n",
1207 | " ktm | \n",
1208 | " brt | \n",
1209 | " drn | \n",
1210 | " pkr | \n",
1211 | " jkp | \n",
1212 | "
\n",
1213 | " \n",
1214 | " \n",
1215 | " \n",
1216 | " 0 | \n",
1217 | " 1 | \n",
1218 | " 2 | \n",
1219 | " 3 | \n",
1220 | " 4 | \n",
1221 | " 5 | \n",
1222 | "
\n",
1223 | " \n",
1224 | " 1 | \n",
1225 | " 6 | \n",
1226 | " 7 | \n",
1227 | " 8 | \n",
1228 | " 9 | \n",
1229 | " 10 | \n",
1230 | "
\n",
1231 | " \n",
1232 | " 2 | \n",
1233 | " 11 | \n",
1234 | " 12 | \n",
1235 | " 13 | \n",
1236 | " 14 | \n",
1237 | " 15 | \n",
1238 | "
\n",
1239 | " \n",
1240 | " 3 | \n",
1241 | " 16 | \n",
1242 | " 17 | \n",
1243 | " 18 | \n",
1244 | " 19 | \n",
1245 | " 20 | \n",
1246 | "
\n",
1247 | " \n",
1248 | " 4 | \n",
1249 | " 21 | \n",
1250 | " 22 | \n",
1251 | " 23 | \n",
1252 | " 24 | \n",
1253 | " 25 | \n",
1254 | "
\n",
1255 | " \n",
1256 | " 5 | \n",
1257 | " 26 | \n",
1258 | " 27 | \n",
1259 | " 28 | \n",
1260 | " 29 | \n",
1261 | " 30 | \n",
1262 | "
\n",
1263 | " \n",
1264 | " 6 | \n",
1265 | " 31 | \n",
1266 | " 32 | \n",
1267 | " 33 | \n",
1268 | " 34 | \n",
1269 | " 35 | \n",
1270 | "
\n",
1271 | " \n",
1272 | " 7 | \n",
1273 | " 36 | \n",
1274 | " 37 | \n",
1275 | " 38 | \n",
1276 | " 39 | \n",
1277 | " 40 | \n",
1278 | "
\n",
1279 | " \n",
1280 | " 8 | \n",
1281 | " 41 | \n",
1282 | " 42 | \n",
1283 | " 43 | \n",
1284 | " 44 | \n",
1285 | " 45 | \n",
1286 | "
\n",
1287 | " \n",
1288 | " 9 | \n",
1289 | " 46 | \n",
1290 | " 47 | \n",
1291 | " 48 | \n",
1292 | " 49 | \n",
1293 | " 50 | \n",
1294 | "
\n",
1295 | " \n",
1296 | " 10 | \n",
1297 | " 51 | \n",
1298 | " 52 | \n",
1299 | " 53 | \n",
1300 | " 54 | \n",
1301 | " 55 | \n",
1302 | "
\n",
1303 | " \n",
1304 | " 11 | \n",
1305 | " 56 | \n",
1306 | " 57 | \n",
1307 | " 58 | \n",
1308 | " 59 | \n",
1309 | " 60 | \n",
1310 | "
\n",
1311 | " \n",
1312 | " 12 | \n",
1313 | " 61 | \n",
1314 | " 62 | \n",
1315 | " 63 | \n",
1316 | " 64 | \n",
1317 | " 65 | \n",
1318 | "
\n",
1319 | " \n",
1320 | " 13 | \n",
1321 | " 66 | \n",
1322 | " 67 | \n",
1323 | " 68 | \n",
1324 | " 69 | \n",
1325 | " 70 | \n",
1326 | "
\n",
1327 | " \n",
1328 | " 14 | \n",
1329 | " 71 | \n",
1330 | " 72 | \n",
1331 | " 73 | \n",
1332 | " 74 | \n",
1333 | " 75 | \n",
1334 | "
\n",
1335 | " \n",
1336 | " 15 | \n",
1337 | " 76 | \n",
1338 | " 77 | \n",
1339 | " 78 | \n",
1340 | " 79 | \n",
1341 | " 80 | \n",
1342 | "
\n",
1343 | " \n",
1344 | " 16 | \n",
1345 | " 81 | \n",
1346 | " 82 | \n",
1347 | " 83 | \n",
1348 | " 84 | \n",
1349 | " 85 | \n",
1350 | "
\n",
1351 | " \n",
1352 | " 17 | \n",
1353 | " 86 | \n",
1354 | " 87 | \n",
1355 | " 88 | \n",
1356 | " 89 | \n",
1357 | " 90 | \n",
1358 | "
\n",
1359 | " \n",
1360 | " 18 | \n",
1361 | " 91 | \n",
1362 | " 92 | \n",
1363 | " 93 | \n",
1364 | " 94 | \n",
1365 | " 95 | \n",
1366 | "
\n",
1367 | " \n",
1368 | " 19 | \n",
1369 | " 96 | \n",
1370 | " 97 | \n",
1371 | " 98 | \n",
1372 | " 99 | \n",
1373 | " 100 | \n",
1374 | "
\n",
1375 | " \n",
1376 | "
\n",
1377 | "
"
1378 | ],
1379 | "text/plain": [
1380 | " ktm brt drn pkr jkp\n",
1381 | "0 1 2 3 4 5\n",
1382 | "1 6 7 8 9 10\n",
1383 | "2 11 12 13 14 15\n",
1384 | "3 16 17 18 19 20\n",
1385 | "4 21 22 23 24 25\n",
1386 | "5 26 27 28 29 30\n",
1387 | "6 31 32 33 34 35\n",
1388 | "7 36 37 38 39 40\n",
1389 | "8 41 42 43 44 45\n",
1390 | "9 46 47 48 49 50\n",
1391 | "10 51 52 53 54 55\n",
1392 | "11 56 57 58 59 60\n",
1393 | "12 61 62 63 64 65\n",
1394 | "13 66 67 68 69 70\n",
1395 | "14 71 72 73 74 75\n",
1396 | "15 76 77 78 79 80\n",
1397 | "16 81 82 83 84 85\n",
1398 | "17 86 87 88 89 90\n",
1399 | "18 91 92 93 94 95\n",
1400 | "19 96 97 98 99 100"
1401 | ]
1402 | },
1403 | "execution_count": 129,
1404 | "metadata": {},
1405 | "output_type": "execute_result"
1406 | }
1407 | ],
1408 | "source": [
1409 | "# construct a dataframe with integer as index\n",
1410 | "mydata = np.arange(1,101).reshape(20,5)\n",
1411 | "col_name = ['ktm','brt','drn','pkr','jkp']\n",
1412 | "df = pd.DataFrame(mydata,index=np.arange(0,20),columns=col_name)\n",
1413 | "df"
1414 | ]
1415 | },
1416 | {
1417 | "cell_type": "code",
1418 | "execution_count": 130,
1419 | "id": "ec7d90c9",
1420 | "metadata": {},
1421 | "outputs": [
1422 | {
1423 | "data": {
1424 | "text/plain": [
1425 | "ktm 96\n",
1426 | "brt 97\n",
1427 | "drn 98\n",
1428 | "pkr 99\n",
1429 | "jkp 100\n",
1430 | "Name: 19, dtype: int32"
1431 | ]
1432 | },
1433 | "execution_count": 130,
1434 | "metadata": {},
1435 | "output_type": "execute_result"
1436 | }
1437 | ],
1438 | "source": [
1439 | "df.iloc[19]"
1440 | ]
1441 | },
1442 | {
1443 | "cell_type": "code",
1444 | "execution_count": 132,
1445 | "id": "6b9af129",
1446 | "metadata": {},
1447 | "outputs": [
1448 | {
1449 | "data": {
1450 | "text/html": [
1451 | "\n",
1452 | "\n",
1465 | "
\n",
1466 | " \n",
1467 | " \n",
1468 | " | \n",
1469 | " ktm | \n",
1470 | " brt | \n",
1471 | " drn | \n",
1472 | " pkr | \n",
1473 | " jkp | \n",
1474 | "
\n",
1475 | " \n",
1476 | " \n",
1477 | " \n",
1478 | " 0 | \n",
1479 | " 1 | \n",
1480 | " 2 | \n",
1481 | " 3 | \n",
1482 | " 4 | \n",
1483 | " 5 | \n",
1484 | "
\n",
1485 | " \n",
1486 | " 1 | \n",
1487 | " 6 | \n",
1488 | " 7 | \n",
1489 | " 8 | \n",
1490 | " 9 | \n",
1491 | " 10 | \n",
1492 | "
\n",
1493 | " \n",
1494 | " 2 | \n",
1495 | " 11 | \n",
1496 | " 12 | \n",
1497 | " 13 | \n",
1498 | " 14 | \n",
1499 | " 15 | \n",
1500 | "
\n",
1501 | " \n",
1502 | " 3 | \n",
1503 | " 16 | \n",
1504 | " 17 | \n",
1505 | " 18 | \n",
1506 | " 19 | \n",
1507 | " 20 | \n",
1508 | "
\n",
1509 | " \n",
1510 | " 4 | \n",
1511 | " 21 | \n",
1512 | " 22 | \n",
1513 | " 23 | \n",
1514 | " 24 | \n",
1515 | " 25 | \n",
1516 | "
\n",
1517 | " \n",
1518 | " 5 | \n",
1519 | " 26 | \n",
1520 | " 27 | \n",
1521 | " 28 | \n",
1522 | " 29 | \n",
1523 | " 30 | \n",
1524 | "
\n",
1525 | " \n",
1526 | " 6 | \n",
1527 | " 31 | \n",
1528 | " 32 | \n",
1529 | " 33 | \n",
1530 | " 34 | \n",
1531 | " 35 | \n",
1532 | "
\n",
1533 | " \n",
1534 | "
\n",
1535 | "
"
1536 | ],
1537 | "text/plain": [
1538 | " ktm brt drn pkr jkp\n",
1539 | "0 1 2 3 4 5\n",
1540 | "1 6 7 8 9 10\n",
1541 | "2 11 12 13 14 15\n",
1542 | "3 16 17 18 19 20\n",
1543 | "4 21 22 23 24 25\n",
1544 | "5 26 27 28 29 30\n",
1545 | "6 31 32 33 34 35"
1546 | ]
1547 | },
1548 | "execution_count": 132,
1549 | "metadata": {},
1550 | "output_type": "execute_result"
1551 | }
1552 | ],
1553 | "source": [
1554 | "df.iloc[0:7]"
1555 | ]
1556 | },
1557 | {
1558 | "cell_type": "code",
1559 | "execution_count": 133,
1560 | "id": "1bb2da82",
1561 | "metadata": {},
1562 | "outputs": [
1563 | {
1564 | "data": {
1565 | "text/html": [
1566 | "\n",
1567 | "\n",
1580 | "
\n",
1581 | " \n",
1582 | " \n",
1583 | " | \n",
1584 | " ktm | \n",
1585 | " brt | \n",
1586 | " drn | \n",
1587 | " pkr | \n",
1588 | " jkp | \n",
1589 | "
\n",
1590 | " \n",
1591 | " \n",
1592 | " \n",
1593 | " 0 | \n",
1594 | " 1 | \n",
1595 | " 2 | \n",
1596 | " 3 | \n",
1597 | " 4 | \n",
1598 | " 5 | \n",
1599 | "
\n",
1600 | " \n",
1601 | " 2 | \n",
1602 | " 11 | \n",
1603 | " 12 | \n",
1604 | " 13 | \n",
1605 | " 14 | \n",
1606 | " 15 | \n",
1607 | "
\n",
1608 | " \n",
1609 | " 4 | \n",
1610 | " 21 | \n",
1611 | " 22 | \n",
1612 | " 23 | \n",
1613 | " 24 | \n",
1614 | " 25 | \n",
1615 | "
\n",
1616 | " \n",
1617 | " 6 | \n",
1618 | " 31 | \n",
1619 | " 32 | \n",
1620 | " 33 | \n",
1621 | " 34 | \n",
1622 | " 35 | \n",
1623 | "
\n",
1624 | " \n",
1625 | "
\n",
1626 | "
"
1627 | ],
1628 | "text/plain": [
1629 | " ktm brt drn pkr jkp\n",
1630 | "0 1 2 3 4 5\n",
1631 | "2 11 12 13 14 15\n",
1632 | "4 21 22 23 24 25\n",
1633 | "6 31 32 33 34 35"
1634 | ]
1635 | },
1636 | "execution_count": 133,
1637 | "metadata": {},
1638 | "output_type": "execute_result"
1639 | }
1640 | ],
1641 | "source": [
1642 | "df.iloc[0:7:2]\n",
1643 | "# slicing \n",
1644 | "# start at 0 stop at 7 and step/ update 2"
1645 | ]
1646 | },
1647 | {
1648 | "cell_type": "code",
1649 | "execution_count": 134,
1650 | "id": "520509f8",
1651 | "metadata": {},
1652 | "outputs": [
1653 | {
1654 | "data": {
1655 | "text/html": [
1656 | "\n",
1657 | "\n",
1670 | "
\n",
1671 | " \n",
1672 | " \n",
1673 | " | \n",
1674 | " ktm | \n",
1675 | " brt | \n",
1676 | " drn | \n",
1677 | " pkr | \n",
1678 | " jkp | \n",
1679 | "
\n",
1680 | " \n",
1681 | " \n",
1682 | " \n",
1683 | " 1 | \n",
1684 | " 6 | \n",
1685 | " 7 | \n",
1686 | " 8 | \n",
1687 | " 9 | \n",
1688 | " 10 | \n",
1689 | "
\n",
1690 | " \n",
1691 | " 3 | \n",
1692 | " 16 | \n",
1693 | " 17 | \n",
1694 | " 18 | \n",
1695 | " 19 | \n",
1696 | " 20 | \n",
1697 | "
\n",
1698 | " \n",
1699 | " 5 | \n",
1700 | " 26 | \n",
1701 | " 27 | \n",
1702 | " 28 | \n",
1703 | " 29 | \n",
1704 | " 30 | \n",
1705 | "
\n",
1706 | " \n",
1707 | "
\n",
1708 | "
"
1709 | ],
1710 | "text/plain": [
1711 | " ktm brt drn pkr jkp\n",
1712 | "1 6 7 8 9 10\n",
1713 | "3 16 17 18 19 20\n",
1714 | "5 26 27 28 29 30"
1715 | ]
1716 | },
1717 | "execution_count": 134,
1718 | "metadata": {},
1719 | "output_type": "execute_result"
1720 | }
1721 | ],
1722 | "source": [
1723 | "df.iloc[1:7:2]\n",
1724 | "# slicing \n",
1725 | "# start at 1 stop at 7 and step/ update 2\n",
1726 | "# 1 3 5"
1727 | ]
1728 | },
1729 | {
1730 | "cell_type": "code",
1731 | "execution_count": 141,
1732 | "id": "5ecd5b90",
1733 | "metadata": {},
1734 | "outputs": [
1735 | {
1736 | "data": {
1737 | "text/plain": [
1738 | "27"
1739 | ]
1740 | },
1741 | "execution_count": 141,
1742 | "metadata": {},
1743 | "output_type": "execute_result"
1744 | }
1745 | ],
1746 | "source": [
1747 | "df.iloc[5]['brt']"
1748 | ]
1749 | },
1750 | {
1751 | "cell_type": "markdown",
1752 | "id": "77500298",
1753 | "metadata": {},
1754 | "source": [
1755 | "- Thank you all\n",
1756 | "- Good times!!"
1757 | ]
1758 | },
1759 | {
1760 | "cell_type": "code",
1761 | "execution_count": null,
1762 | "id": "2147b897",
1763 | "metadata": {},
1764 | "outputs": [],
1765 | "source": []
1766 | }
1767 | ],
1768 | "metadata": {
1769 | "kernelspec": {
1770 | "display_name": "Python 3 (ipykernel)",
1771 | "language": "python",
1772 | "name": "python3"
1773 | },
1774 | "language_info": {
1775 | "codemirror_mode": {
1776 | "name": "ipython",
1777 | "version": 3
1778 | },
1779 | "file_extension": ".py",
1780 | "mimetype": "text/x-python",
1781 | "name": "python",
1782 | "nbconvert_exporter": "python",
1783 | "pygments_lexer": "ipython3",
1784 | "version": "3.11.5"
1785 | }
1786 | },
1787 | "nbformat": 4,
1788 | "nbformat_minor": 5
1789 | }
1790 |
--------------------------------------------------------------------------------
/1_hello_world.py:
--------------------------------------------------------------------------------
1 | # hello world program
2 | # print used for displaying content
3 | # use of end
4 | print("Hello, World!")
5 | print("This is demo content")
6 | print("Run this program")
7 | print("The following line will come in same line", end=' ')
8 | print("Use of end=' ' is the reason", end= '\n')
9 | print("Use of end='--' is the reason next content comes after charcters in end", end= '--')
10 | print("Analyze the output! Happy Learning")
11 |
--------------------------------------------------------------------------------
/2_variables.py:
--------------------------------------------------------------------------------
1 | # variables are containers for storing data values
2 | # assigning some values is all that's needed for variable creation
3 | # no need to declare with the type
4 | # determines the type depending upon the assigned value
5 | # integer
6 | # type function is used for finding the type of variable
7 | a = 22
8 | print(a)
9 | print(type(a))
10 |
11 | # float
12 | b = 22.5
13 | print(b)
14 | print(type(b))
15 |
16 | # string
17 | c = "this is test string"
18 | print(c)
19 | print(type(c))
20 |
21 | # boolean
22 | d = True
23 | print(d)
24 | print(type(d))
25 |
26 | # Naming conventions
27 | a = 22 # is allowed
28 | a1 = 22 # is allowed
29 | # 1a = 22 # not allowed, uncomment and check
30 | # a one =22 # not allowed, uncomment and check
31 | a_one = 22 #allowed
32 |
33 | # Remember:
34 | # variable name must start with a letter or the underscore character
35 | # can't start with number
36 | # only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
37 | # is case sensitive i.e a1 and A1 is different
38 | # no keywords could be used as variable name
39 | # Total keywords count is 35
--------------------------------------------------------------------------------
/3_user_input.py:
--------------------------------------------------------------------------------
1 | # input function is used
2 | # it returns string
3 | number = input("Enter any number ")
4 | print(number)
5 | print(type(number))
6 |
7 | # typecast to other type as:
8 | number = int(input("Enter any number "))
9 | print(number)
10 | print(type(number))
11 |
12 | number = float(input("Enter any number "))
13 | print(number)
14 | print(type(number))
15 |
--------------------------------------------------------------------------------
/4_arithmetic_operators.py:
--------------------------------------------------------------------------------
1 | # check entering any two numbers
2 | # input function being used along with prompting the user
3 | # typecasting to integer type
4 | # addition
5 | num1 = int(input("Enter first number "))
6 | num2 = int(input("Enter second number "))
7 | result = num1 + num2
8 | print(f"Sum of {num1} and {num2} is {result}")
9 |
10 | # subtraction
11 | result = num1 - num2
12 | print(f"Difference of {num1} and {num2} is {result}")
13 |
14 | # multiplication
15 | result = num1 * num2
16 | print(f"Product of {num1} and {num2} is {result}")
17 |
18 | # division
19 | result = num1 / num2
20 | print(f"Division of {num1} and {num2} is {result}")
21 |
22 | # floor division
23 | result = num1 // num2
24 | print(f"Floor division of {num1} and {num2} is {result}")
25 |
26 | # remainder
27 | result = num1 % num2
28 | print(f"Remainder of {num1} and {num2} is {result}")
29 |
30 |
--------------------------------------------------------------------------------
/5_conditional_statements.py:
--------------------------------------------------------------------------------
1 | # if condition_is_true:
2 | # execute this
3 | a = 22
4 | print(f"a in this case is {a}")
5 | if a > 20:
6 | print("Inside if block")
7 | # comment previous if
8 | # uncomment following if for analyzing the concept and operations
9 | a = 5
10 | print(f"a in this case is {a}")
11 | if a > 20:
12 | print("current value of a is greater than 20")
13 | print("Condition satisfied")
14 | print("Inside if block")
15 | # 5 is not greater than 20 so the program flow don't enter the if block
16 | # add another if for another condition
17 | if a < 20:
18 | print("Inside second if block")
19 |
20 | # if else
21 | # comment out the above statements
22 | a = 22
23 | print(f"a in this case is {a}")
24 | if a > 20:
25 | print(">20")
26 | else:
27 | print("<20")
28 | # change the value of a to 10 from 22 and execute the program
29 | # multiple conditions
30 | # if elif
31 | # comment out the previous ones
32 | a = 17
33 | print(f"a in this case is {a}")
34 | if a > 20:
35 | print(">20")
36 | elif a > 15:
37 | print(">15")
38 | elif a > 10:
39 | print(">10")
40 | else:
41 | print("<=10")
42 | # NOTE:
43 | # change the value of a to 22 and execute
44 | # change the value of a to 12 and execute
45 | # change the value of a to 5 and execute
46 | # GOOD TIMES
--------------------------------------------------------------------------------
/6_string_info.py:
--------------------------------------------------------------------------------
1 | # string is similar to array
2 | # array is collection of similar data type
3 | # string is a collection of characters, alphabets or words
4 | # one of the primitive data structures
5 | # building blocks for data manipulation
6 | # features:
7 | # supports indexing
8 | # String indexing in Python is zero-based
9 | # first element is indexed 0, second is indexed one and so on
10 | # strings are immutable i.e. they can't be changed after they are created
11 | # supports slicing
12 | some_string = 'this is test string'
13 | # strings can be denoted using 'string' ,"string","""string""",'''string'''
14 | # i.e. string in Python can be easily created using single, double, or even triple quotes
15 | # 'roshan',"roshan","""roshan"""
16 | # defined inside str class
17 | print(some_string)
18 | print(type(some_string))
19 | # indexing
20 | print(some_string[0])
21 | print(some_string[1])
22 | # negative indexing is supported
23 | print((some_string[-1]))
24 | print((some_string[-2]))
25 | # immutable
26 | some_string = 'new text assigned'
27 | print(f"Changed string is {some_string}")
28 | # not allowed
29 | # uncomment and run the following code which will generate error message: TypeError: 'str' object does not support item assignment
30 | # some_string[0] ='a'
31 | # slicing
32 | # sub string can be accessed using slicing
33 | # [start:end:step]
34 | print(some_string[::]) #display whole string
35 | print(some_string[1::]) # from index 1 to end
36 | print(some_string[0:5]) # from index 0 to index 4 i.e. excluding 5
37 | print(some_string[::2]) # from 0 to end showing each second characters
38 | print(some_string[1:7:2]) # from 1 to 6 excluding 7 and step 2
39 | print(some_string[::-1]) # reverse the string
40 |
--------------------------------------------------------------------------------
/7_string methods_1:
--------------------------------------------------------------------------------
1 | # upper lower title casefold index count replace center strip isalnum
2 | # python string methods is a collection of in-built Python functions
3 | # string methods in python
4 | some_string = "heLLo it's mE RoSHAN Shrestha"
5 | print(f"Original string is: {some_string}")
6 | #1 upper()
7 | print("\t\tDemo of upper() method")
8 | print(some_string.upper()) # all characters is converted to upper case (capital letter)
9 | # 2 lower()
10 | print("\t\tDemo of lower() method")
11 | print(some_string.lower()) # all characters is converted to lower case (small letter)
12 | # 3 title()
13 | print("\t\tDemo of title() method")
14 | print(some_string.title()) # first letter of all words is converted to capital letter
15 | # 4 casefold()
16 | print("\t\tDemo of casefold() method")
17 | print(some_string.casefold()) #converts all charcaters into lower case
18 | # suited for caseless matching for unicode characters
19 | # NOTE: str.lower() is suited for caseless matching of ASCII characters.
20 | # 5 index()
21 | # returns the lowest index in which that character resided
22 | print("\t\tDemo of index() method")
23 | print(some_string.index('R'))
24 | print(some_string.index('e'))
25 | # Raise value error if not found
26 | # ValueError: substring not found
27 | # uncomment and check this one
28 | # print(some_string.index('X'))
29 | # 6 count()
30 | # returns the total number of characters that is found in the string
31 | print("\t\tDemo of count() method")
32 | print(f"original string is {some_string}")
33 | print(some_string.count('e'))
34 | # returns 0 if not found
35 | print(some_string.count('X'))
36 | # 7 replace()
37 | # replace the character with the provided character/word
38 | print("\t\tDemo of replace() method")
39 | print(some_string.replace('e','Nepalese'))
40 | # can be controlled how many places those updates are required
41 | print(some_string.replace('e','Nepalese',1))
42 | print(some_string.replace('e','Nepalese',0))
43 | # 8 center()
44 | print("\t\tDemo of center() method")
45 | # center align the string, using a specified character (space is default) as the fill character.
46 | temp_string = some_string.center(92,'-')
47 | print(temp_string)
48 | # 9 strip()
49 | # Return a copy of the string with leading and trailing whitespace removed.
50 | print("\t\tDemo of center() method")
51 | some_string = " heLLo it's mE RoSHAN Shrestha "
52 | print(some_string)
53 | print(f"string after strip is \n{some_string.strip()}")
54 |
55 | some_string = "heLLo it's mE RoSHAN Shresth"
56 | print(some_string)
57 | print(f"string after strip h is \n{some_string.strip('h')}")
58 | # 10 isalnum()
59 | # Return True if the string is an alpha-numeric string, False otherwise.
60 | print("\t\tDemo of isalnum() method")
61 | some_string = "Roshanshrestha123"
62 | print(some_string.isalnum())
63 | some_string = "123" # True
64 | print(some_string.isalnum())
65 | some_string = "Roshan shrestha123"
66 | print(some_string.isalnum()) # False since whitespace is there
67 |
68 |
--------------------------------------------------------------------------------
/8_string_methods2.py:
--------------------------------------------------------------------------------
1 | # python string methods is a collection of in-built Python functions
2 | # isnumeric(),find(),isalpha(),endswith(), split(),istitle(),islower(),rstrip(),lstrip(),swapcase()
3 |
4 | # string methods in python
5 | # 1 isnumeric()
6 | # Return True if the string is a numeric string otherwise False
7 | print("\t\tDemo of isnumeric()")
8 | some_string = "123"
9 | print(some_string.isnumeric())
10 | some_string = "hello123"
11 | print(some_string.isnumeric())
12 | some_string = "III123"
13 | print(some_string.isnumeric())
14 | # 2 find()
15 | # Return the lowest index in S where substring sub is found and -1 if not found
16 | some_string = 'this is test rest nest'
17 | print("\t\tDemo of find()")
18 | print(some_string.find('i'))
19 | print(some_string.find('X'))
20 | # 3 isalpha()
21 | # Return True if the string is an alphabetic string, False otherwise
22 | some_string = 'thisistestrestnest'
23 | print("\t\tDemo of isalpha()")
24 | print(some_string.isalpha())
25 | some_string = 'thisistestrestnest1'
26 | print(some_string.isalpha())
27 | # 4 endswith()
28 | # Return True if S ends with the specified suffix, False otherwise
29 | print("\t\tDemo of endswith()")
30 | some_string = 'roshan shrestha'
31 | print(some_string.endswith('a'))
32 | print(some_string.endswith('x'))
33 | # 5 split()
34 | # Return a list of the substrings in the string, using sep as the separator string.
35 | print("\t\tDemo of split()")
36 | some_string = 'hello there come here'
37 | print(some_string.split('e'))
38 | print(some_string.split('o'))
39 | # 6 istitle()
40 | # Return True if the string is a title-cased string, False otherwise.
41 | some_string = 'Roshan Shrestha'
42 | print("\t\tDemo of istitle()")
43 | print(some_string.istitle())
44 | some_string = 'Roshan shrestha'
45 | print(some_string.istitle())
46 | # 7 islower()
47 | # Return True if the string is a lowercase string, False otherwise.
48 | some_string = 'roshan shrestha'
49 | print("\t\tDemo of istitle()")
50 | print(some_string.islower())
51 |
52 | some_string = 'roshan sHrestha'
53 | print(some_string.islower())
54 | # 8 rstrip()
55 | # rstrip() method removes any trailing characters (characters at the end a string)
56 | some_string = 'this is demo '
57 | print("\t\tDemo of rstrip()")
58 | print(some_string.rstrip())
59 | some_string = 'this is demo'
60 | print(some_string.rstrip('o'))
61 | # 9 lstrip()
62 | # Return a copy of the string with leading whitespace removed.
63 | # If chars is given and not None, remove characters in chars instead.
64 | print("\t\tDemo of lstrip()")
65 | print(some_string.lstrip('t'))
66 | # 10 swapcase()
67 | # Convert uppercase characters to lowercase and lowercase characters to uppercase.
68 | some_string = 'RoSHan sHreSTHa'
69 | print("\t\tDemo of swapcase()")
70 | print(some_string.swapcase())
71 |
--------------------------------------------------------------------------------
/9_pandas_series.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "d2e4630a",
6 | "metadata": {},
7 | "source": [
8 | "# pandas "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "5d389bb5",
14 | "metadata": {},
15 | "source": [
16 | "- refers towards panel data and python data analysis\n",
17 | "- developed in 2008 by McKinney\n",
18 | "- Installation:\n",
19 | " - if PIP and python is already installed in your machine: \n",
20 | " - pip install pandas\n",
21 | "- Note:\n",
22 | " - python distribution like anaconda already have pandas installed\n",
23 | "\n",
24 | "- data structure in python:\n",
25 | " - data series\n",
26 | " one dimension\n",
27 | " like column in a table\n",
28 | " - dataframe\n",
29 | " 2 dimension\n",
30 | " table with row and column\n"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 59,
36 | "id": "fc26af5c",
37 | "metadata": {},
38 | "outputs": [],
39 | "source": [
40 | "# importing pandas\n",
41 | "import pandas as pd"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": 2,
47 | "id": "fc12e045",
48 | "metadata": {},
49 | "outputs": [
50 | {
51 | "data": {
52 | "text/plain": [
53 | "'2.2.0'"
54 | ]
55 | },
56 | "execution_count": 2,
57 | "metadata": {},
58 | "output_type": "execute_result"
59 | }
60 | ],
61 | "source": [
62 | "# check version that will verify the availability of pandas\n",
63 | "pd.__version__\n",
64 | "# this can vary depending upon the version installed in your machine"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "id": "0ab4c4f6",
70 | "metadata": {},
71 | "source": [
72 | "# Series"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "id": "09637d54",
78 | "metadata": {},
79 | "source": [
80 | "- list as data"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 62,
86 | "id": "53394228",
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "data": {
91 | "text/plain": [
92 | "0 11\n",
93 | "1 22\n",
94 | "2 33\n",
95 | "3 55\n",
96 | "4 11\n",
97 | "5 66\n",
98 | "6 77\n",
99 | "dtype: int64"
100 | ]
101 | },
102 | "execution_count": 62,
103 | "metadata": {},
104 | "output_type": "execute_result"
105 | }
106 | ],
107 | "source": [
108 | "\n",
109 | "mydata = [11,22,33,55,11,66,77]\n",
110 | "dataseries = pd.Series(mydata)\n",
111 | "dataseries\n",
112 | "# Note: 'S' in Series is upper case\n",
113 | "# by default the index begins from zeroth index\n",
114 | "# so 0 1 2 etcetera is being displayed"
115 | ]
116 | },
117 | {
118 | "cell_type": "markdown",
119 | "id": "140f97b3",
120 | "metadata": {},
121 | "source": [
122 | "- indexing(positive indexing) is allowed"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 63,
128 | "id": "20def019",
129 | "metadata": {},
130 | "outputs": [
131 | {
132 | "data": {
133 | "text/plain": [
134 | "(11, 22)"
135 | ]
136 | },
137 | "execution_count": 63,
138 | "metadata": {},
139 | "output_type": "execute_result"
140 | }
141 | ],
142 | "source": [
143 | "dataseries[0], dataseries[1]\n"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "id": "f6fb5be0",
149 | "metadata": {},
150 | "source": [
151 | "- negative indexing is not allowed"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": 64,
157 | "id": "1d78f1b4",
158 | "metadata": {},
159 | "outputs": [
160 | {
161 | "ename": "KeyError",
162 | "evalue": "-1",
163 | "output_type": "error",
164 | "traceback": [
165 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
166 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
167 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:413\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 412\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 413\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_range\u001b[38;5;241m.\u001b[39mindex(new_key)\n\u001b[0;32m 414\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
168 | "\u001b[1;31mValueError\u001b[0m: -1 is not in range",
169 | "\nThe above exception was the direct cause of the following exception:\n",
170 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
171 | "Cell \u001b[1;32mIn[64], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m dataseries[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]\n",
172 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\series.py:1111\u001b[0m, in \u001b[0;36mSeries.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1108\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[key]\n\u001b[0;32m 1110\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m key_is_scalar:\n\u001b[1;32m-> 1111\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_value(key)\n\u001b[0;32m 1113\u001b[0m \u001b[38;5;66;03m# Convert generator to list before going through hashable part\u001b[39;00m\n\u001b[0;32m 1114\u001b[0m \u001b[38;5;66;03m# (We will iterate through the generator there to check for slices)\u001b[39;00m\n\u001b[0;32m 1115\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_iterator(key):\n",
173 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\series.py:1227\u001b[0m, in \u001b[0;36mSeries._get_value\u001b[1;34m(self, label, takeable)\u001b[0m\n\u001b[0;32m 1224\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[label]\n\u001b[0;32m 1226\u001b[0m \u001b[38;5;66;03m# Similar to Index.get_value, but we do not fall back to positional\u001b[39;00m\n\u001b[1;32m-> 1227\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mget_loc(label)\n\u001b[0;32m 1229\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_integer(loc):\n\u001b[0;32m 1230\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[loc]\n",
174 | "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:415\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 413\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_range\u001b[38;5;241m.\u001b[39mindex(new_key)\n\u001b[0;32m 414\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m--> 415\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Hashable):\n\u001b[0;32m 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n",
175 | "\u001b[1;31mKeyError\u001b[0m: -1"
176 | ]
177 | }
178 | ],
179 | "source": [
180 | "dataseries[-1]"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 65,
186 | "id": "582b9c2d",
187 | "metadata": {},
188 | "outputs": [
189 | {
190 | "data": {
191 | "text/plain": [
192 | "0 11\n",
193 | "1 22\n",
194 | "2 33\n",
195 | "3 55\n",
196 | "4 11\n",
197 | "5 66\n",
198 | "6 77\n",
199 | "dtype: int64"
200 | ]
201 | },
202 | "execution_count": 65,
203 | "metadata": {},
204 | "output_type": "execute_result"
205 | }
206 | ],
207 | "source": [
208 | "dataseries"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "id": "ce3b5960",
214 | "metadata": {},
215 | "source": [
216 | "- slicing is allowed\n",
217 | "- we can access the sub portion of the series"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 66,
223 | "id": "3a805537",
224 | "metadata": {},
225 | "outputs": [
226 | {
227 | "data": {
228 | "text/plain": [
229 | "1 22\n",
230 | "2 33\n",
231 | "3 55\n",
232 | "4 11\n",
233 | "dtype: int64"
234 | ]
235 | },
236 | "execution_count": 66,
237 | "metadata": {},
238 | "output_type": "execute_result"
239 | }
240 | ],
241 | "source": [
242 | "dataseries[1:5]"
243 | ]
244 | },
245 | {
246 | "cell_type": "code",
247 | "execution_count": 67,
248 | "id": "cbfc91da",
249 | "metadata": {},
250 | "outputs": [
251 | {
252 | "data": {
253 | "text/plain": [
254 | "0 11\n",
255 | "1 22\n",
256 | "2 33\n",
257 | "3 55\n",
258 | "4 11\n",
259 | "5 66\n",
260 | "6 77\n",
261 | "dtype: int64"
262 | ]
263 | },
264 | "execution_count": 67,
265 | "metadata": {},
266 | "output_type": "execute_result"
267 | }
268 | ],
269 | "source": [
270 | "dataseries[::] "
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": 68,
276 | "id": "c5cada95",
277 | "metadata": {},
278 | "outputs": [
279 | {
280 | "data": {
281 | "text/plain": [
282 | "1 22\n",
283 | "2 33\n",
284 | "3 55\n",
285 | "4 11\n",
286 | "5 66\n",
287 | "dtype: int64"
288 | ]
289 | },
290 | "execution_count": 68,
291 | "metadata": {},
292 | "output_type": "execute_result"
293 | }
294 | ],
295 | "source": [
296 | "dataseries[1:6] \n",
297 | "# start at 1 stop at 6 not including stop point"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 69,
303 | "id": "3287af2f",
304 | "metadata": {},
305 | "outputs": [
306 | {
307 | "data": {
308 | "text/plain": [
309 | "1 22\n",
310 | "3 55\n",
311 | "5 66\n",
312 | "dtype: int64"
313 | ]
314 | },
315 | "execution_count": 69,
316 | "metadata": {},
317 | "output_type": "execute_result"
318 | }
319 | ],
320 | "source": [
321 | "dataseries[1:6:2] \n",
322 | "# start at 1 stop at 6 not including stop point update by step 2\n",
323 | "# begins from 1\n",
324 | "# add 2 on that, becomes 1 + 2 =3\n",
325 | "# add 2 on current value, becomes 3 + 2= 5"
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "execution_count": 23,
331 | "id": "82206792",
332 | "metadata": {},
333 | "outputs": [],
334 | "source": [
335 | "# update detail using slicing"
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": 70,
341 | "id": "0d07f499",
342 | "metadata": {},
343 | "outputs": [
344 | {
345 | "data": {
346 | "text/plain": [
347 | "0 11\n",
348 | "1 100\n",
349 | "2 101\n",
350 | "3 102\n",
351 | "4 11\n",
352 | "5 66\n",
353 | "6 77\n",
354 | "dtype: int64"
355 | ]
356 | },
357 | "execution_count": 70,
358 | "metadata": {},
359 | "output_type": "execute_result"
360 | }
361 | ],
362 | "source": [
363 | "dataseries[1:4]=[100,101,102]\n",
364 | "dataseries\n",
365 | "# index 1 to 3 is update to 100 101 and 102"
366 | ]
367 | },
368 | {
369 | "cell_type": "code",
370 | "execution_count": 71,
371 | "id": "a9d894c2",
372 | "metadata": {},
373 | "outputs": [
374 | {
375 | "data": {
376 | "text/plain": [
377 | "0 11\n",
378 | "1 999\n",
379 | "2 999\n",
380 | "3 999\n",
381 | "4 11\n",
382 | "5 66\n",
383 | "6 77\n",
384 | "dtype: int64"
385 | ]
386 | },
387 | "execution_count": 71,
388 | "metadata": {},
389 | "output_type": "execute_result"
390 | }
391 | ],
392 | "source": [
393 | "# update using same value\n",
394 | "dataseries[1:4] = 999\n",
395 | "dataseries\n",
396 | "# index 1 to 3 is updated to same value 999"
397 | ]
398 | },
399 | {
400 | "cell_type": "code",
401 | "execution_count": 72,
402 | "id": "93028011",
403 | "metadata": {},
404 | "outputs": [
405 | {
406 | "data": {
407 | "text/plain": [
408 | "ind1 22\n",
409 | "ind2 33\n",
410 | "ind3 444\n",
411 | "ind4 55\n",
412 | "dtype: int64"
413 | ]
414 | },
415 | "execution_count": 72,
416 | "metadata": {},
417 | "output_type": "execute_result"
418 | }
419 | ],
420 | "source": [
421 | "# we have been working with default index\n",
422 | "# we can customize the index \n",
423 | "mydata = [22,33,444,55]\n",
424 | "series_with_own_index= pd.Series(mydata,['ind1','ind2','ind3','ind4'])\n",
425 | "series_with_own_index"
426 | ]
427 | },
428 | {
429 | "cell_type": "code",
430 | "execution_count": null,
431 | "id": "fa43a4ab",
432 | "metadata": {},
433 | "outputs": [],
434 | "source": [
435 | "# keys and values of Series"
436 | ]
437 | },
438 | {
439 | "cell_type": "code",
440 | "execution_count": 40,
441 | "id": "a1f617c3",
442 | "metadata": {},
443 | "outputs": [
444 | {
445 | "data": {
446 | "text/plain": [
447 | "Index(['ind1', 'ind2', 'ind3', 'ind4'], dtype='object')"
448 | ]
449 | },
450 | "execution_count": 40,
451 | "metadata": {},
452 | "output_type": "execute_result"
453 | }
454 | ],
455 | "source": [
456 | "series_with_own_index.keys()"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 41,
462 | "id": "68cacbdd",
463 | "metadata": {},
464 | "outputs": [
465 | {
466 | "data": {
467 | "text/plain": [
468 | "array([ 22, 33, 444, 55], dtype=int64)"
469 | ]
470 | },
471 | "execution_count": 41,
472 | "metadata": {},
473 | "output_type": "execute_result"
474 | }
475 | ],
476 | "source": [
477 | "series_with_own_index.values"
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "execution_count": 75,
483 | "id": "19303ab7",
484 | "metadata": {},
485 | "outputs": [
486 | {
487 | "data": {
488 | "text/plain": [
489 | "ind1 22\n",
490 | "ind2 33\n",
491 | "ind4 55\n",
492 | "ind3 444\n",
493 | "dtype: int64"
494 | ]
495 | },
496 | "execution_count": 75,
497 | "metadata": {},
498 | "output_type": "execute_result"
499 | }
500 | ],
501 | "source": [
502 | "series_with_own_index.sort_values()\n",
503 | "# ascending order "
504 | ]
505 | },
506 | {
507 | "cell_type": "code",
508 | "execution_count": 78,
509 | "id": "88d77856",
510 | "metadata": {},
511 | "outputs": [
512 | {
513 | "data": {
514 | "text/plain": [
515 | "ind3 444\n",
516 | "ind4 55\n",
517 | "ind2 33\n",
518 | "ind1 22\n",
519 | "dtype: int64"
520 | ]
521 | },
522 | "execution_count": 78,
523 | "metadata": {},
524 | "output_type": "execute_result"
525 | }
526 | ],
527 | "source": [
528 | "# descending order\n",
529 | "series_with_own_index.sort_values(ascending=False)"
530 | ]
531 | },
532 | {
533 | "cell_type": "code",
534 | "execution_count": 79,
535 | "id": "a06c0821",
536 | "metadata": {},
537 | "outputs": [
538 | {
539 | "data": {
540 | "text/plain": [
541 | "554"
542 | ]
543 | },
544 | "execution_count": 79,
545 | "metadata": {},
546 | "output_type": "execute_result"
547 | }
548 | ],
549 | "source": [
550 | "series_with_own_index.sum()"
551 | ]
552 | },
553 | {
554 | "cell_type": "code",
555 | "execution_count": 80,
556 | "id": "c5045c40",
557 | "metadata": {},
558 | "outputs": [
559 | {
560 | "data": {
561 | "text/plain": [
562 | "444"
563 | ]
564 | },
565 | "execution_count": 80,
566 | "metadata": {},
567 | "output_type": "execute_result"
568 | }
569 | ],
570 | "source": [
571 | "series_with_own_index.max()"
572 | ]
573 | },
574 | {
575 | "cell_type": "code",
576 | "execution_count": 81,
577 | "id": "c54d40fa",
578 | "metadata": {},
579 | "outputs": [
580 | {
581 | "data": {
582 | "text/plain": [
583 | "22"
584 | ]
585 | },
586 | "execution_count": 81,
587 | "metadata": {},
588 | "output_type": "execute_result"
589 | }
590 | ],
591 | "source": [
592 | "series_with_own_index.min()"
593 | ]
594 | },
595 | {
596 | "cell_type": "code",
597 | "execution_count": 82,
598 | "id": "6a7b3c4c",
599 | "metadata": {},
600 | "outputs": [
601 | {
602 | "data": {
603 | "text/plain": [
604 | "138.5"
605 | ]
606 | },
607 | "execution_count": 82,
608 | "metadata": {},
609 | "output_type": "execute_result"
610 | }
611 | ],
612 | "source": [
613 | "series_with_own_index.mean()"
614 | ]
615 | },
616 | {
617 | "cell_type": "code",
618 | "execution_count": 83,
619 | "id": "1e82a8e3",
620 | "metadata": {},
621 | "outputs": [
622 | {
623 | "data": {
624 | "text/plain": [
625 | "44.0"
626 | ]
627 | },
628 | "execution_count": 83,
629 | "metadata": {},
630 | "output_type": "execute_result"
631 | }
632 | ],
633 | "source": [
634 | "series_with_own_index.median()"
635 | ]
636 | },
637 | {
638 | "cell_type": "code",
639 | "execution_count": 84,
640 | "id": "8c29a330",
641 | "metadata": {},
642 | "outputs": [
643 | {
644 | "data": {
645 | "text/plain": [
646 | "ind1 22\n",
647 | "ind2 33\n",
648 | "ind3 444\n",
649 | "ind4 55\n",
650 | "dtype: int64"
651 | ]
652 | },
653 | "execution_count": 84,
654 | "metadata": {},
655 | "output_type": "execute_result"
656 | }
657 | ],
658 | "source": [
659 | "series_with_own_index"
660 | ]
661 | },
662 | {
663 | "cell_type": "code",
664 | "execution_count": 86,
665 | "id": "15ae7b28",
666 | "metadata": {},
667 | "outputs": [],
668 | "source": [
669 | "# index that has highest value\n",
670 | "# in this example 444 is highest value with index ind3 that is at \n",
671 | "# index number 2 (since indexing begins from zeroth index)"
672 | ]
673 | },
674 | {
675 | "cell_type": "code",
676 | "execution_count": 85,
677 | "id": "1e336c0d",
678 | "metadata": {},
679 | "outputs": [
680 | {
681 | "data": {
682 | "text/plain": [
683 | "2"
684 | ]
685 | },
686 | "execution_count": 85,
687 | "metadata": {},
688 | "output_type": "execute_result"
689 | }
690 | ],
691 | "source": [
692 | "series_with_own_index.argmax()"
693 | ]
694 | },
695 | {
696 | "cell_type": "code",
697 | "execution_count": 87,
698 | "id": "9d51e9c6",
699 | "metadata": {},
700 | "outputs": [
701 | {
702 | "data": {
703 | "text/plain": [
704 | "0 22\n",
705 | "1 44\n",
706 | "2 33\n",
707 | "3 44\n",
708 | "4 11\n",
709 | "5 34\n",
710 | "6 11\n",
711 | "dtype: int64"
712 | ]
713 | },
714 | "execution_count": 87,
715 | "metadata": {},
716 | "output_type": "execute_result"
717 | }
718 | ],
719 | "source": [
720 | "test_data = [22,44,33,44,11,34,11]\n",
721 | "user_series = pd.Series(test_data)\n",
722 | "user_series"
723 | ]
724 | },
725 | {
726 | "cell_type": "code",
727 | "execution_count": 90,
728 | "id": "8434f471",
729 | "metadata": {},
730 | "outputs": [],
731 | "source": [
732 | "# argmax displays the first occurance of the index with highest value"
733 | ]
734 | },
735 | {
736 | "cell_type": "code",
737 | "execution_count": 52,
738 | "id": "d3b528a0",
739 | "metadata": {},
740 | "outputs": [
741 | {
742 | "data": {
743 | "text/plain": [
744 | "1"
745 | ]
746 | },
747 | "execution_count": 52,
748 | "metadata": {},
749 | "output_type": "execute_result"
750 | }
751 | ],
752 | "source": [
753 | "user_series.argmax()"
754 | ]
755 | },
756 | {
757 | "cell_type": "code",
758 | "execution_count": 92,
759 | "id": "c02ea034",
760 | "metadata": {},
761 | "outputs": [
762 | {
763 | "data": {
764 | "text/plain": [
765 | "7"
766 | ]
767 | },
768 | "execution_count": 92,
769 | "metadata": {},
770 | "output_type": "execute_result"
771 | }
772 | ],
773 | "source": [
774 | "user_series.count()"
775 | ]
776 | },
777 | {
778 | "cell_type": "code",
779 | "execution_count": 93,
780 | "id": "eda09ded",
781 | "metadata": {},
782 | "outputs": [],
783 | "source": [
784 | "# dictionary as data"
785 | ]
786 | },
787 | {
788 | "cell_type": "code",
789 | "execution_count": 94,
790 | "id": "616ce88d",
791 | "metadata": {},
792 | "outputs": [],
793 | "source": [
794 | "mydata = {\n",
795 | " 'ram':77,\n",
796 | " 'shyam':81,\n",
797 | " 'sita':91\n",
798 | "}"
799 | ]
800 | },
801 | {
802 | "cell_type": "code",
803 | "execution_count": 96,
804 | "id": "f2bc4f98",
805 | "metadata": {},
806 | "outputs": [
807 | {
808 | "data": {
809 | "text/plain": [
810 | "ram 77\n",
811 | "shyam 81\n",
812 | "sita 91\n",
813 | "dtype: int64"
814 | ]
815 | },
816 | "execution_count": 96,
817 | "metadata": {},
818 | "output_type": "execute_result"
819 | }
820 | ],
821 | "source": [
822 | "myseries = pd.Series(mydata)\n",
823 | "myseries"
824 | ]
825 | },
826 | {
827 | "cell_type": "code",
828 | "execution_count": 97,
829 | "id": "05bbc0ac",
830 | "metadata": {},
831 | "outputs": [
832 | {
833 | "data": {
834 | "text/plain": [
835 | "ram 77\n",
836 | "sita 91\n",
837 | "dtype: int64"
838 | ]
839 | },
840 | "execution_count": 97,
841 | "metadata": {},
842 | "output_type": "execute_result"
843 | }
844 | ],
845 | "source": [
846 | "# use selected index only\n",
847 | "# let's say we don't want to access all of the index\n",
848 | "# we can customize it this way:\n",
849 | "myseries = pd.Series(mydata,index=['ram','sita'])\n",
850 | "myseries"
851 | ]
852 | },
853 | {
854 | "cell_type": "code",
855 | "execution_count": null,
856 | "id": "59f52e0d",
857 | "metadata": {},
858 | "outputs": [],
859 | "source": [
860 | "# Have great moments ahead\n",
861 | "# Good times"
862 | ]
863 | }
864 | ],
865 | "metadata": {
866 | "kernelspec": {
867 | "display_name": "Python 3 (ipykernel)",
868 | "language": "python",
869 | "name": "python3"
870 | },
871 | "language_info": {
872 | "codemirror_mode": {
873 | "name": "ipython",
874 | "version": 3
875 | },
876 | "file_extension": ".py",
877 | "mimetype": "text/x-python",
878 | "name": "python",
879 | "nbconvert_exporter": "python",
880 | "pygments_lexer": "ipython3",
881 | "version": "3.11.5"
882 | }
883 | },
884 | "nbformat": 4,
885 | "nbformat_minor": 5
886 | }
887 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # python-practice
2 | programs for understanding python in simple and efficient way!
3 | - 1_hello_world.py
4 | writing the first program
5 | demonstrating the print function and end parameter
6 |
7 | - 2_variables.py
8 | what is variables?
9 | Naming conventions
10 | type function
11 |
12 | - 3_user_input.py
13 | taking user input
14 | type conversion
15 |
16 | - 4_arithmetic_operators.py
17 | + ,- ,* ,/ ,//, %
18 |
19 | - 5_conditional_statements.py
20 | if
21 | multiple if
22 | if else
23 | if elif else
24 | - 6_string_info.py
25 | strings
26 | features of string
27 | indexing, negative indexing, immutability, slicing
28 |
29 | - 7_string methods_1
30 | string inbuilt methods in python
31 | upper(), lower(), title(), casefold(), index(), count(), replace(), center(), strip(), isalnum()
32 | - 8_string_methods2.py
33 | isnumeric(),find(),isalpha(),endswith(), split(),istitle(),islower(),rstrip(),lstrip(),swapcase()
34 | - 9_pandas_series
35 | how series work
36 | indexing and slicing in series
37 | customized index
38 | methods like mean median sum max sort ascending descending
39 | list and dictionar objects used for series etcetera
40 | - 10_pandas_DataFrame
41 | constructing dataframe using dictionary ,series, ndarray etcetera
42 | columns and index access
43 | accessing selected columns
44 | loc and iloc
45 |
46 |
--------------------------------------------------------------------------------