`\n",
321 | " \n",
322 | " See Also\n",
323 | " --------\n",
324 | " DataFrame.at : Access a single value for a row/column label pair\n",
325 | " DataFrame.iloc : Access group of rows and columns by integer position(s)\n",
326 | " DataFrame.xs : Returns a cross-section (row(s) or column(s)) from the\n",
327 | " Series/DataFrame.\n",
328 | " Series.loc : Access group of values using labels\n",
329 | " \n",
330 | " Examples\n",
331 | " --------\n",
332 | " **Getting values**\n",
333 | " \n",
334 | " >>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],\n",
335 | " ... index=['cobra', 'viper', 'sidewinder'],\n",
336 | " ... columns=['max_speed', 'shield'])\n",
337 | " >>> df\n",
338 | " max_speed shield\n",
339 | " cobra 1 2\n",
340 | " viper 4 5\n",
341 | " sidewinder 7 8\n",
342 | " \n",
343 | " Single label. Note this returns the row as a Series.\n",
344 | " \n",
345 | " >>> df.loc['viper']\n",
346 | " max_speed 4\n",
347 | " shield 5\n",
348 | " Name: viper, dtype: int64\n",
349 | " \n",
350 | " List of labels. Note using ``[[]]`` returns a DataFrame.\n",
351 | " \n",
352 | " >>> df.loc[['viper', 'sidewinder']]\n",
353 | " max_speed shield\n",
354 | " viper 4 5\n",
355 | " sidewinder 7 8\n",
356 | " \n",
357 | " Single label for row and column\n",
358 | " \n",
359 | " >>> df.loc['cobra', 'shield']\n",
360 | " 2\n",
361 | " \n",
362 | " Slice with labels for row and single label for column. As mentioned\n",
363 | " above, note that both the start and stop of the slice are included.\n",
364 | " \n",
365 | " >>> df.loc['cobra':'viper', 'max_speed']\n",
366 | " cobra 1\n",
367 | " viper 4\n",
368 | " Name: max_speed, dtype: int64\n",
369 | " \n",
370 | " Boolean list with the same length as the row axis\n",
371 | " \n",
372 | " >>> df.loc[[False, False, True]]\n",
373 | " max_speed shield\n",
374 | " sidewinder 7 8\n",
375 | " \n",
376 | " Conditional that returns a boolean Series\n",
377 | " \n",
378 | " >>> df.loc[df['shield'] > 6]\n",
379 | " max_speed shield\n",
380 | " sidewinder 7 8\n",
381 | " \n",
382 | " Conditional that returns a boolean Series with column labels specified\n",
383 | " \n",
384 | " >>> df.loc[df['shield'] > 6, ['max_speed']]\n",
385 | " max_speed\n",
386 | " sidewinder 7\n",
387 | " \n",
388 | " Callable that returns a boolean Series\n",
389 | " \n",
390 | " >>> df.loc[lambda df: df['shield'] == 8]\n",
391 | " max_speed shield\n",
392 | " sidewinder 7 8\n",
393 | " \n",
394 | " **Setting values**\n",
395 | " \n",
396 | " Set value for all items matching the list of labels\n",
397 | " \n",
398 | " >>> df.loc[['viper', 'sidewinder'], ['shield']] = 50\n",
399 | " >>> df\n",
400 | " max_speed shield\n",
401 | " cobra 1 2\n",
402 | " viper 4 50\n",
403 | " sidewinder 7 50\n",
404 | " \n",
405 | " Set value for an entire row\n",
406 | " \n",
407 | " >>> df.loc['cobra'] = 10\n",
408 | " >>> df\n",
409 | " max_speed shield\n",
410 | " cobra 10 10\n",
411 | " viper 4 50\n",
412 | " sidewinder 7 50\n",
413 | " \n",
414 | " Set value for an entire column\n",
415 | " \n",
416 | " >>> df.loc[:, 'max_speed'] = 30\n",
417 | " >>> df\n",
418 | " max_speed shield\n",
419 | " cobra 30 10\n",
420 | " viper 30 50\n",
421 | " sidewinder 30 50\n",
422 | " \n",
423 | " Set value for rows matching callable condition\n",
424 | " \n",
425 | " >>> df.loc[df['shield'] > 35] = 0\n",
426 | " >>> df\n",
427 | " max_speed shield\n",
428 | " cobra 30 10\n",
429 | " viper 0 0\n",
430 | " sidewinder 0 0\n",
431 | " \n",
432 | " **Getting values on a DataFrame with an index that has integer labels**\n",
433 | " \n",
434 | " Another example using integers for the index\n",
435 | " \n",
436 | " >>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],\n",
437 | " ... index=[7, 8, 9], columns=['max_speed', 'shield'])\n",
438 | " >>> df\n",
439 | " max_speed shield\n",
440 | " 7 1 2\n",
441 | " 8 4 5\n",
442 | " 9 7 8\n",
443 | " \n",
444 | " Slice with integer labels for rows. As mentioned above, note that both\n",
445 | " the start and stop of the slice are included.\n",
446 | " \n",
447 | " >>> df.loc[7:9]\n",
448 | " max_speed shield\n",
449 | " 7 1 2\n",
450 | " 8 4 5\n",
451 | " 9 7 8\n",
452 | " \n",
453 | " **Getting values with a MultiIndex**\n",
454 | " \n",
455 | " A number of examples using a DataFrame with a MultiIndex\n",
456 | " \n",
457 | " >>> tuples = [\n",
458 | " ... ('cobra', 'mark i'), ('cobra', 'mark ii'),\n",
459 | " ... ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),\n",
460 | " ... ('viper', 'mark ii'), ('viper', 'mark iii')\n",
461 | " ... ]\n",
462 | " >>> index = pd.MultiIndex.from_tuples(tuples)\n",
463 | " >>> values = [[12, 2], [0, 4], [10, 20],\n",
464 | " ... [1, 4], [7, 1], [16, 36]]\n",
465 | " >>> df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)\n",
466 | " >>> df\n",
467 | " max_speed shield\n",
468 | " cobra mark i 12 2\n",
469 | " mark ii 0 4\n",
470 | " sidewinder mark i 10 20\n",
471 | " mark ii 1 4\n",
472 | " viper mark ii 7 1\n",
473 | " mark iii 16 36\n",
474 | " \n",
475 | " Single label. Note this returns a DataFrame with a single index.\n",
476 | " \n",
477 | " >>> df.loc['cobra']\n",
478 | " max_speed shield\n",
479 | " mark i 12 2\n",
480 | " mark ii 0 4\n",
481 | " \n",
482 | " Single index tuple. Note this returns a Series.\n",
483 | " \n",
484 | " >>> df.loc[('cobra', 'mark ii')]\n",
485 | " max_speed 0\n",
486 | " shield 4\n",
487 | " Name: (cobra, mark ii), dtype: int64\n",
488 | " \n",
489 | " Single label for row and column. Similar to passing in a tuple, this\n",
490 | " returns a Series.\n",
491 | " \n",
492 | " >>> df.loc['cobra', 'mark i']\n",
493 | " max_speed 12\n",
494 | " shield 2\n",
495 | " Name: (cobra, mark i), dtype: int64\n",
496 | " \n",
497 | " Single tuple. Note using ``[[]]`` returns a DataFrame.\n",
498 | " \n",
499 | " >>> df.loc[[('cobra', 'mark ii')]]\n",
500 | " max_speed shield\n",
501 | " cobra mark ii 0 4\n",
502 | " \n",
503 | " Single tuple for the index with a single label for the column\n",
504 | " \n",
505 | " >>> df.loc[('cobra', 'mark i'), 'shield']\n",
506 | " 2\n",
507 | " \n",
508 | " Slice from index tuple to single label\n",
509 | " \n",
510 | " >>> df.loc[('cobra', 'mark i'):'viper']\n",
511 | " max_speed shield\n",
512 | " cobra mark i 12 2\n",
513 | " mark ii 0 4\n",
514 | " sidewinder mark i 10 20\n",
515 | " mark ii 1 4\n",
516 | " viper mark ii 7 1\n",
517 | " mark iii 16 36\n",
518 | " \n",
519 | " Slice from index tuple to index tuple\n",
520 | " \n",
521 | " >>> df.loc[('cobra', 'mark i'):('viper', 'mark ii')]\n",
522 | " max_speed shield\n",
523 | " cobra mark i 12 2\n",
524 | " mark ii 0 4\n",
525 | " sidewinder mark i 10 20\n",
526 | " mark ii 1 4\n",
527 | " viper mark ii 7 1\n",
528 | " \n",
529 | " Raises\n",
530 | " ------\n",
531 | " KeyError:\n",
532 | " when any items are not found\n",
533 | "\n"
534 | ]
535 | }
536 | ],
537 | "source": [
538 | "help(pd.Series.loc)"
539 | ]
540 | },
541 | {
542 | "cell_type": "markdown",
543 | "metadata": {},
544 | "source": [
545 | "\n",
546 | "## Selection\n",
547 | ""
548 | ]
549 | },
550 | {
551 | "cell_type": "code",
552 | "execution_count": 25,
553 | "metadata": {},
554 | "outputs": [
555 | {
556 | "data": {
557 | "text/html": [
558 | "\n",
559 | "\n",
572 | "
\n",
573 | " \n",
574 | " \n",
575 | " | \n",
576 | " Football_team | \n",
577 | " Footballer | \n",
578 | " money | \n",
579 | "
\n",
580 | " \n",
581 | " \n",
582 | " \n",
583 | " 0 | \n",
584 | " barcelona | \n",
585 | " messi | \n",
586 | " 30.0 | \n",
587 | "
\n",
588 | " \n",
589 | " 1 | \n",
590 | " real_madrid | \n",
591 | " ramos | \n",
592 | " 20.0 | \n",
593 | "
\n",
594 | " \n",
595 | "
\n",
596 | "
"
597 | ],
598 | "text/plain": [
599 | " Football_team Footballer money\n",
600 | "0 barcelona messi 30.0\n",
601 | "1 real_madrid ramos 20.0"
602 | ]
603 | },
604 | "execution_count": 25,
605 | "metadata": {},
606 | "output_type": "execute_result"
607 | }
608 | ],
609 | "source": [
610 | "df"
611 | ]
612 | },
613 | {
614 | "cell_type": "code",
615 | "execution_count": 26,
616 | "metadata": {},
617 | "outputs": [
618 | {
619 | "data": {
620 | "text/plain": [
621 | "0 30.0\n",
622 | "1 20.0\n",
623 | "Name: money, dtype: float64"
624 | ]
625 | },
626 | "execution_count": 26,
627 | "metadata": {},
628 | "output_type": "execute_result"
629 | }
630 | ],
631 | "source": [
632 | "df[\"money\"]"
633 | ]
634 | },
635 | {
636 | "cell_type": "code",
637 | "execution_count": 28,
638 | "metadata": {},
639 | "outputs": [
640 | {
641 | "data": {
642 | "text/html": [
643 | "\n",
644 | "\n",
657 | "
\n",
658 | " \n",
659 | " \n",
660 | " | \n",
661 | " Football_team | \n",
662 | " Footballer | \n",
663 | " money | \n",
664 | "
\n",
665 | " \n",
666 | " \n",
667 | " \n",
668 | " 0 | \n",
669 | " barcelona | \n",
670 | " messi | \n",
671 | " 30.0 | \n",
672 | "
\n",
673 | " \n",
674 | "
\n",
675 | "
"
676 | ],
677 | "text/plain": [
678 | " Football_team Footballer money\n",
679 | "0 barcelona messi 30.0"
680 | ]
681 | },
682 | "execution_count": 28,
683 | "metadata": {},
684 | "output_type": "execute_result"
685 | }
686 | ],
687 | "source": [
688 | "df[:1]"
689 | ]
690 | },
691 | {
692 | "cell_type": "code",
693 | "execution_count": 29,
694 | "metadata": {},
695 | "outputs": [
696 | {
697 | "data": {
698 | "text/plain": [
699 | "'ramos'"
700 | ]
701 | },
702 | "execution_count": 29,
703 | "metadata": {},
704 | "output_type": "execute_result"
705 | }
706 | ],
707 | "source": [
708 | "df.iloc[1,1]"
709 | ]
710 | },
711 | {
712 | "cell_type": "code",
713 | "execution_count": 30,
714 | "metadata": {},
715 | "outputs": [
716 | {
717 | "data": {
718 | "text/plain": [
719 | "'ramos'"
720 | ]
721 | },
722 | "execution_count": 30,
723 | "metadata": {},
724 | "output_type": "execute_result"
725 | }
726 | ],
727 | "source": [
728 | "df.loc[1,\"Footballer\"]"
729 | ]
730 | },
731 | {
732 | "cell_type": "code",
733 | "execution_count": 35,
734 | "metadata": {},
735 | "outputs": [],
736 | "source": [
737 | "#df.ix[1,1]"
738 | ]
739 | },
740 | {
741 | "cell_type": "code",
742 | "execution_count": 38,
743 | "metadata": {},
744 | "outputs": [
745 | {
746 | "data": {
747 | "text/html": [
748 | "\n",
749 | "\n",
762 | "
\n",
763 | " \n",
764 | " \n",
765 | " | \n",
766 | " Football_team | \n",
767 | " Footballer | \n",
768 | " money | \n",
769 | "
\n",
770 | " \n",
771 | " \n",
772 | " \n",
773 | " 0 | \n",
774 | " barcelona | \n",
775 | " messi | \n",
776 | " 30.0 | \n",
777 | "
\n",
778 | " \n",
779 | " 1 | \n",
780 | " real_madrid | \n",
781 | " ramos | \n",
782 | " 20.0 | \n",
783 | "
\n",
784 | " \n",
785 | "
\n",
786 | "
"
787 | ],
788 | "text/plain": [
789 | " Football_team Footballer money\n",
790 | "0 barcelona messi 30.0\n",
791 | "1 real_madrid ramos 20.0"
792 | ]
793 | },
794 | "execution_count": 38,
795 | "metadata": {},
796 | "output_type": "execute_result"
797 | }
798 | ],
799 | "source": [
800 | "df"
801 | ]
802 | },
803 | {
804 | "cell_type": "code",
805 | "execution_count": 39,
806 | "metadata": {},
807 | "outputs": [
808 | {
809 | "data": {
810 | "text/html": [
811 | "\n",
812 | "\n",
825 | "
\n",
826 | " \n",
827 | " \n",
828 | " | \n",
829 | " Football_team | \n",
830 | " Footballer | \n",
831 | " money | \n",
832 | "
\n",
833 | " \n",
834 | " \n",
835 | " \n",
836 | " 1 | \n",
837 | " real_madrid | \n",
838 | " ramos | \n",
839 | " 20.0 | \n",
840 | "
\n",
841 | " \n",
842 | "
\n",
843 | "
"
844 | ],
845 | "text/plain": [
846 | " Football_team Footballer money\n",
847 | "1 real_madrid ramos 20.0"
848 | ]
849 | },
850 | "execution_count": 39,
851 | "metadata": {},
852 | "output_type": "execute_result"
853 | }
854 | ],
855 | "source": [
856 | "filter_ = 25 > df[\"money\"]\n",
857 | "df[filter_]"
858 | ]
859 | },
860 | {
861 | "cell_type": "code",
862 | "execution_count": 44,
863 | "metadata": {},
864 | "outputs": [
865 | {
866 | "data": {
867 | "text/plain": [
868 | "c1 10\n",
869 | "c2 20\n",
870 | "c3 30\n",
871 | "dtype: int64"
872 | ]
873 | },
874 | "execution_count": 44,
875 | "metadata": {},
876 | "output_type": "execute_result"
877 | }
878 | ],
879 | "source": [
880 | "series"
881 | ]
882 | },
883 | {
884 | "cell_type": "code",
885 | "execution_count": 46,
886 | "metadata": {},
887 | "outputs": [
888 | {
889 | "data": {
890 | "text/plain": [
891 | "c1 10\n",
892 | "c2 100\n",
893 | "c3 30\n",
894 | "dtype: int64"
895 | ]
896 | },
897 | "execution_count": 46,
898 | "metadata": {},
899 | "output_type": "execute_result"
900 | }
901 | ],
902 | "source": [
903 | "series[\"c2\"] = 100\n",
904 | "series"
905 | ]
906 | },
907 | {
908 | "cell_type": "markdown",
909 | "metadata": {},
910 | "source": [
911 | "\n",
912 | "## Dropping\n",
913 | ""
914 | ]
915 | },
916 | {
917 | "cell_type": "code",
918 | "execution_count": 57,
919 | "metadata": {},
920 | "outputs": [
921 | {
922 | "data": {
923 | "text/html": [
924 | "\n",
925 | "\n",
938 | "
\n",
939 | " \n",
940 | " \n",
941 | " | \n",
942 | " Football_team | \n",
943 | " Footballer | \n",
944 | " money | \n",
945 | "
\n",
946 | " \n",
947 | " \n",
948 | " \n",
949 | " 0 | \n",
950 | " barcelona | \n",
951 | " messi | \n",
952 | " 30.0 | \n",
953 | "
\n",
954 | " \n",
955 | " 1 | \n",
956 | " real_madrid | \n",
957 | " ramos | \n",
958 | " 20.0 | \n",
959 | "
\n",
960 | " \n",
961 | "
\n",
962 | "
"
963 | ],
964 | "text/plain": [
965 | " Football_team Footballer money\n",
966 | "0 barcelona messi 30.0\n",
967 | "1 real_madrid ramos 20.0"
968 | ]
969 | },
970 | "execution_count": 57,
971 | "metadata": {},
972 | "output_type": "execute_result"
973 | }
974 | ],
975 | "source": [
976 | "data = {\"Football_team\":[\"barcelona\", \"real_madrid\"],\n",
977 | " \"Footballer\":[\"messi\",\"ramos\"],\n",
978 | " \"money\":[30.0,20.0]}\n",
979 | "df = pd.DataFrame(data, columns=[\"Football_team\", \"Footballer\",\"money\" ])\n",
980 | "df"
981 | ]
982 | },
983 | {
984 | "cell_type": "code",
985 | "execution_count": 59,
986 | "metadata": {},
987 | "outputs": [
988 | {
989 | "data": {
990 | "text/html": [
991 | "\n",
992 | "\n",
1005 | "
\n",
1006 | " \n",
1007 | " \n",
1008 | " | \n",
1009 | " Football_team | \n",
1010 | " Footballer | \n",
1011 | " money | \n",
1012 | "
\n",
1013 | " \n",
1014 | " \n",
1015 | " \n",
1016 | " 1 | \n",
1017 | " real_madrid | \n",
1018 | " ramos | \n",
1019 | " 20.0 | \n",
1020 | "
\n",
1021 | " \n",
1022 | "
\n",
1023 | "
"
1024 | ],
1025 | "text/plain": [
1026 | " Football_team Footballer money\n",
1027 | "1 real_madrid ramos 20.0"
1028 | ]
1029 | },
1030 | "execution_count": 59,
1031 | "metadata": {},
1032 | "output_type": "execute_result"
1033 | }
1034 | ],
1035 | "source": [
1036 | "df.drop([0],inplace = True)\n",
1037 | "df"
1038 | ]
1039 | },
1040 | {
1041 | "cell_type": "code",
1042 | "execution_count": 60,
1043 | "metadata": {},
1044 | "outputs": [
1045 | {
1046 | "data": {
1047 | "text/html": [
1048 | "\n",
1049 | "\n",
1062 | "
\n",
1063 | " \n",
1064 | " \n",
1065 | " | \n",
1066 | " Football_team | \n",
1067 | " Footballer | \n",
1068 | "
\n",
1069 | " \n",
1070 | " \n",
1071 | " \n",
1072 | " 1 | \n",
1073 | " real_madrid | \n",
1074 | " ramos | \n",
1075 | "
\n",
1076 | " \n",
1077 | "
\n",
1078 | "
"
1079 | ],
1080 | "text/plain": [
1081 | " Football_team Footballer\n",
1082 | "1 real_madrid ramos"
1083 | ]
1084 | },
1085 | "execution_count": 60,
1086 | "metadata": {},
1087 | "output_type": "execute_result"
1088 | }
1089 | ],
1090 | "source": [
1091 | "df.drop([\"money\"],axis = 1,inplace = True)\n",
1092 | "df"
1093 | ]
1094 | },
1095 | {
1096 | "cell_type": "markdown",
1097 | "metadata": {},
1098 | "source": [
1099 | "\n",
1100 | "## Sort and Rank\n",
1101 | ""
1102 | ]
1103 | },
1104 | {
1105 | "cell_type": "code",
1106 | "execution_count": 61,
1107 | "metadata": {},
1108 | "outputs": [
1109 | {
1110 | "data": {
1111 | "text/html": [
1112 | "\n",
1113 | "\n",
1126 | "
\n",
1127 | " \n",
1128 | " \n",
1129 | " | \n",
1130 | " Football_team | \n",
1131 | " Footballer | \n",
1132 | " money | \n",
1133 | "
\n",
1134 | " \n",
1135 | " \n",
1136 | " \n",
1137 | " 0 | \n",
1138 | " barcelona | \n",
1139 | " messi | \n",
1140 | " 30.0 | \n",
1141 | "
\n",
1142 | " \n",
1143 | " 1 | \n",
1144 | " real_madrid | \n",
1145 | " ramos | \n",
1146 | " 20.0 | \n",
1147 | "
\n",
1148 | " \n",
1149 | "
\n",
1150 | "
"
1151 | ],
1152 | "text/plain": [
1153 | " Football_team Footballer money\n",
1154 | "0 barcelona messi 30.0\n",
1155 | "1 real_madrid ramos 20.0"
1156 | ]
1157 | },
1158 | "execution_count": 61,
1159 | "metadata": {},
1160 | "output_type": "execute_result"
1161 | }
1162 | ],
1163 | "source": [
1164 | "data = {\"Football_team\":[\"barcelona\", \"real_madrid\"],\n",
1165 | " \"Footballer\":[\"messi\",\"ramos\"],\n",
1166 | " \"money\":[30.0,20.0]}\n",
1167 | "df = pd.DataFrame(data, columns=[\"Football_team\", \"Footballer\",\"money\" ])\n",
1168 | "df"
1169 | ]
1170 | },
1171 | {
1172 | "cell_type": "code",
1173 | "execution_count": 68,
1174 | "metadata": {},
1175 | "outputs": [
1176 | {
1177 | "data": {
1178 | "text/html": [
1179 | "\n",
1180 | "\n",
1193 | "
\n",
1194 | " \n",
1195 | " \n",
1196 | " | \n",
1197 | " Football_team | \n",
1198 | " Footballer | \n",
1199 | " money | \n",
1200 | "
\n",
1201 | " \n",
1202 | " \n",
1203 | " \n",
1204 | " 1 | \n",
1205 | " real_madrid | \n",
1206 | " ramos | \n",
1207 | " 20.0 | \n",
1208 | "
\n",
1209 | " \n",
1210 | " 0 | \n",
1211 | " barcelona | \n",
1212 | " messi | \n",
1213 | " 30.0 | \n",
1214 | "
\n",
1215 | " \n",
1216 | "
\n",
1217 | "
"
1218 | ],
1219 | "text/plain": [
1220 | " Football_team Footballer money\n",
1221 | "1 real_madrid ramos 20.0\n",
1222 | "0 barcelona messi 30.0"
1223 | ]
1224 | },
1225 | "execution_count": 68,
1226 | "metadata": {},
1227 | "output_type": "execute_result"
1228 | }
1229 | ],
1230 | "source": [
1231 | "df = df.sort_values(by = \"money\")\n",
1232 | "df"
1233 | ]
1234 | },
1235 | {
1236 | "cell_type": "code",
1237 | "execution_count": 69,
1238 | "metadata": {},
1239 | "outputs": [
1240 | {
1241 | "data": {
1242 | "text/html": [
1243 | "\n",
1244 | "\n",
1257 | "
\n",
1258 | " \n",
1259 | " \n",
1260 | " | \n",
1261 | " Football_team | \n",
1262 | " Footballer | \n",
1263 | " money | \n",
1264 | "
\n",
1265 | " \n",
1266 | " \n",
1267 | " \n",
1268 | " 0 | \n",
1269 | " barcelona | \n",
1270 | " messi | \n",
1271 | " 30.0 | \n",
1272 | "
\n",
1273 | " \n",
1274 | " 1 | \n",
1275 | " real_madrid | \n",
1276 | " ramos | \n",
1277 | " 20.0 | \n",
1278 | "
\n",
1279 | " \n",
1280 | "
\n",
1281 | "
"
1282 | ],
1283 | "text/plain": [
1284 | " Football_team Footballer money\n",
1285 | "0 barcelona messi 30.0\n",
1286 | "1 real_madrid ramos 20.0"
1287 | ]
1288 | },
1289 | "execution_count": 69,
1290 | "metadata": {},
1291 | "output_type": "execute_result"
1292 | }
1293 | ],
1294 | "source": [
1295 | "df = df.sort_index()\n",
1296 | "df"
1297 | ]
1298 | },
1299 | {
1300 | "cell_type": "code",
1301 | "execution_count": 71,
1302 | "metadata": {},
1303 | "outputs": [
1304 | {
1305 | "data": {
1306 | "text/html": [
1307 | "\n",
1308 | "\n",
1321 | "
\n",
1322 | " \n",
1323 | " \n",
1324 | " | \n",
1325 | " Football_team | \n",
1326 | " Footballer | \n",
1327 | " money | \n",
1328 | "
\n",
1329 | " \n",
1330 | " \n",
1331 | " \n",
1332 | " 0 | \n",
1333 | " 1.0 | \n",
1334 | " 1.0 | \n",
1335 | " 2.0 | \n",
1336 | "
\n",
1337 | " \n",
1338 | " 1 | \n",
1339 | " 2.0 | \n",
1340 | " 2.0 | \n",
1341 | " 1.0 | \n",
1342 | "
\n",
1343 | " \n",
1344 | "
\n",
1345 | "
"
1346 | ],
1347 | "text/plain": [
1348 | " Football_team Footballer money\n",
1349 | "0 1.0 1.0 2.0\n",
1350 | "1 2.0 2.0 1.0"
1351 | ]
1352 | },
1353 | "execution_count": 71,
1354 | "metadata": {},
1355 | "output_type": "execute_result"
1356 | }
1357 | ],
1358 | "source": [
1359 | "df.rank()"
1360 | ]
1361 | },
1362 | {
1363 | "cell_type": "markdown",
1364 | "metadata": {},
1365 | "source": [
1366 | "\n",
1367 | "## Retrieving Series/DataFrame Information\n",
1368 | ""
1369 | ]
1370 | },
1371 | {
1372 | "cell_type": "code",
1373 | "execution_count": 72,
1374 | "metadata": {},
1375 | "outputs": [
1376 | {
1377 | "data": {
1378 | "text/html": [
1379 | "\n",
1380 | "\n",
1393 | "
\n",
1394 | " \n",
1395 | " \n",
1396 | " | \n",
1397 | " Football_team | \n",
1398 | " Footballer | \n",
1399 | " money | \n",
1400 | "
\n",
1401 | " \n",
1402 | " \n",
1403 | " \n",
1404 | " 0 | \n",
1405 | " barcelona | \n",
1406 | " messi | \n",
1407 | " 30.0 | \n",
1408 | "
\n",
1409 | " \n",
1410 | " 1 | \n",
1411 | " real_madrid | \n",
1412 | " ramos | \n",
1413 | " 20.0 | \n",
1414 | "
\n",
1415 | " \n",
1416 | "
\n",
1417 | "
"
1418 | ],
1419 | "text/plain": [
1420 | " Football_team Footballer money\n",
1421 | "0 barcelona messi 30.0\n",
1422 | "1 real_madrid ramos 20.0"
1423 | ]
1424 | },
1425 | "execution_count": 72,
1426 | "metadata": {},
1427 | "output_type": "execute_result"
1428 | }
1429 | ],
1430 | "source": [
1431 | "df"
1432 | ]
1433 | },
1434 | {
1435 | "cell_type": "code",
1436 | "execution_count": 73,
1437 | "metadata": {},
1438 | "outputs": [
1439 | {
1440 | "data": {
1441 | "text/plain": [
1442 | "(2, 3)"
1443 | ]
1444 | },
1445 | "execution_count": 73,
1446 | "metadata": {},
1447 | "output_type": "execute_result"
1448 | }
1449 | ],
1450 | "source": [
1451 | "df.shape"
1452 | ]
1453 | },
1454 | {
1455 | "cell_type": "code",
1456 | "execution_count": 74,
1457 | "metadata": {},
1458 | "outputs": [
1459 | {
1460 | "data": {
1461 | "text/plain": [
1462 | "Int64Index([0, 1], dtype='int64')"
1463 | ]
1464 | },
1465 | "execution_count": 74,
1466 | "metadata": {},
1467 | "output_type": "execute_result"
1468 | }
1469 | ],
1470 | "source": [
1471 | "df.index"
1472 | ]
1473 | },
1474 | {
1475 | "cell_type": "code",
1476 | "execution_count": 75,
1477 | "metadata": {},
1478 | "outputs": [
1479 | {
1480 | "data": {
1481 | "text/plain": [
1482 | "Index(['Football_team', 'Footballer', 'money'], dtype='object')"
1483 | ]
1484 | },
1485 | "execution_count": 75,
1486 | "metadata": {},
1487 | "output_type": "execute_result"
1488 | }
1489 | ],
1490 | "source": [
1491 | "df.columns"
1492 | ]
1493 | },
1494 | {
1495 | "cell_type": "code",
1496 | "execution_count": 76,
1497 | "metadata": {},
1498 | "outputs": [
1499 | {
1500 | "name": "stdout",
1501 | "output_type": "stream",
1502 | "text": [
1503 | "\n",
1504 | "Int64Index: 2 entries, 0 to 1\n",
1505 | "Data columns (total 3 columns):\n",
1506 | "Football_team 2 non-null object\n",
1507 | "Footballer 2 non-null object\n",
1508 | "money 2 non-null float64\n",
1509 | "dtypes: float64(1), object(2)\n",
1510 | "memory usage: 64.0+ bytes\n"
1511 | ]
1512 | }
1513 | ],
1514 | "source": [
1515 | "df.info()"
1516 | ]
1517 | },
1518 | {
1519 | "cell_type": "code",
1520 | "execution_count": 78,
1521 | "metadata": {},
1522 | "outputs": [
1523 | {
1524 | "data": {
1525 | "text/plain": [
1526 | "Football_team 2\n",
1527 | "Footballer 2\n",
1528 | "money 2\n",
1529 | "dtype: int64"
1530 | ]
1531 | },
1532 | "execution_count": 78,
1533 | "metadata": {},
1534 | "output_type": "execute_result"
1535 | }
1536 | ],
1537 | "source": [
1538 | "df.count() # nan"
1539 | ]
1540 | },
1541 | {
1542 | "cell_type": "code",
1543 | "execution_count": 80,
1544 | "metadata": {},
1545 | "outputs": [
1546 | {
1547 | "data": {
1548 | "text/html": [
1549 | "\n",
1550 | "\n",
1563 | "
\n",
1564 | " \n",
1565 | " \n",
1566 | " | \n",
1567 | " Football_team | \n",
1568 | " Footballer | \n",
1569 | " money | \n",
1570 | "
\n",
1571 | " \n",
1572 | " \n",
1573 | " \n",
1574 | " 0 | \n",
1575 | " barcelona | \n",
1576 | " messi | \n",
1577 | " 30.0 | \n",
1578 | "
\n",
1579 | " \n",
1580 | " 1 | \n",
1581 | " real_madrid | \n",
1582 | " ramos | \n",
1583 | " 20.0 | \n",
1584 | "
\n",
1585 | " \n",
1586 | "
\n",
1587 | "
"
1588 | ],
1589 | "text/plain": [
1590 | " Football_team Footballer money\n",
1591 | "0 barcelona messi 30.0\n",
1592 | "1 real_madrid ramos 20.0"
1593 | ]
1594 | },
1595 | "execution_count": 80,
1596 | "metadata": {},
1597 | "output_type": "execute_result"
1598 | }
1599 | ],
1600 | "source": [
1601 | "df"
1602 | ]
1603 | },
1604 | {
1605 | "cell_type": "code",
1606 | "execution_count": 79,
1607 | "metadata": {},
1608 | "outputs": [
1609 | {
1610 | "data": {
1611 | "text/plain": [
1612 | "Football_team barcelonareal_madrid\n",
1613 | "Footballer messiramos\n",
1614 | "money 50\n",
1615 | "dtype: object"
1616 | ]
1617 | },
1618 | "execution_count": 79,
1619 | "metadata": {},
1620 | "output_type": "execute_result"
1621 | }
1622 | ],
1623 | "source": [
1624 | "df.sum()"
1625 | ]
1626 | },
1627 | {
1628 | "cell_type": "code",
1629 | "execution_count": 82,
1630 | "metadata": {},
1631 | "outputs": [
1632 | {
1633 | "data": {
1634 | "text/plain": [
1635 | "Football_team real_madrid\n",
1636 | "Footballer ramos\n",
1637 | "money 30\n",
1638 | "dtype: object"
1639 | ]
1640 | },
1641 | "execution_count": 82,
1642 | "metadata": {},
1643 | "output_type": "execute_result"
1644 | }
1645 | ],
1646 | "source": [
1647 | "df.max()"
1648 | ]
1649 | },
1650 | {
1651 | "cell_type": "code",
1652 | "execution_count": 89,
1653 | "metadata": {},
1654 | "outputs": [
1655 | {
1656 | "data": {
1657 | "text/plain": [
1658 | "1"
1659 | ]
1660 | },
1661 | "execution_count": 89,
1662 | "metadata": {},
1663 | "output_type": "execute_result"
1664 | }
1665 | ],
1666 | "source": [
1667 | "df.index.argmax()"
1668 | ]
1669 | },
1670 | {
1671 | "cell_type": "code",
1672 | "execution_count": 90,
1673 | "metadata": {},
1674 | "outputs": [
1675 | {
1676 | "data": {
1677 | "text/html": [
1678 | "\n",
1679 | "\n",
1692 | "
\n",
1693 | " \n",
1694 | " \n",
1695 | " | \n",
1696 | " money | \n",
1697 | "
\n",
1698 | " \n",
1699 | " \n",
1700 | " \n",
1701 | " count | \n",
1702 | " 2.000000 | \n",
1703 | "
\n",
1704 | " \n",
1705 | " mean | \n",
1706 | " 25.000000 | \n",
1707 | "
\n",
1708 | " \n",
1709 | " std | \n",
1710 | " 7.071068 | \n",
1711 | "
\n",
1712 | " \n",
1713 | " min | \n",
1714 | " 20.000000 | \n",
1715 | "
\n",
1716 | " \n",
1717 | " 25% | \n",
1718 | " 22.500000 | \n",
1719 | "
\n",
1720 | " \n",
1721 | " 50% | \n",
1722 | " 25.000000 | \n",
1723 | "
\n",
1724 | " \n",
1725 | " 75% | \n",
1726 | " 27.500000 | \n",
1727 | "
\n",
1728 | " \n",
1729 | " max | \n",
1730 | " 30.000000 | \n",
1731 | "
\n",
1732 | " \n",
1733 | "
\n",
1734 | "
"
1735 | ],
1736 | "text/plain": [
1737 | " money\n",
1738 | "count 2.000000\n",
1739 | "mean 25.000000\n",
1740 | "std 7.071068\n",
1741 | "min 20.000000\n",
1742 | "25% 22.500000\n",
1743 | "50% 25.000000\n",
1744 | "75% 27.500000\n",
1745 | "max 30.000000"
1746 | ]
1747 | },
1748 | "execution_count": 90,
1749 | "metadata": {},
1750 | "output_type": "execute_result"
1751 | }
1752 | ],
1753 | "source": [
1754 | "df.describe()"
1755 | ]
1756 | },
1757 | {
1758 | "cell_type": "code",
1759 | "execution_count": 92,
1760 | "metadata": {},
1761 | "outputs": [
1762 | {
1763 | "data": {
1764 | "text/plain": [
1765 | "money 25.0\n",
1766 | "dtype: float64"
1767 | ]
1768 | },
1769 | "execution_count": 92,
1770 | "metadata": {},
1771 | "output_type": "execute_result"
1772 | }
1773 | ],
1774 | "source": [
1775 | "df.mean()"
1776 | ]
1777 | },
1778 | {
1779 | "cell_type": "code",
1780 | "execution_count": 93,
1781 | "metadata": {},
1782 | "outputs": [
1783 | {
1784 | "data": {
1785 | "text/plain": [
1786 | "money 25.0\n",
1787 | "dtype: float64"
1788 | ]
1789 | },
1790 | "execution_count": 93,
1791 | "metadata": {},
1792 | "output_type": "execute_result"
1793 | }
1794 | ],
1795 | "source": [
1796 | "df.median()"
1797 | ]
1798 | },
1799 | {
1800 | "cell_type": "markdown",
1801 | "metadata": {},
1802 | "source": [
1803 | "\n",
1804 | "## Applying Functions\n",
1805 | ""
1806 | ]
1807 | },
1808 | {
1809 | "cell_type": "code",
1810 | "execution_count": 94,
1811 | "metadata": {},
1812 | "outputs": [
1813 | {
1814 | "data": {
1815 | "text/html": [
1816 | "\n",
1817 | "\n",
1830 | "
\n",
1831 | " \n",
1832 | " \n",
1833 | " | \n",
1834 | " Football_team | \n",
1835 | " Footballer | \n",
1836 | " money | \n",
1837 | "
\n",
1838 | " \n",
1839 | " \n",
1840 | " \n",
1841 | " 0 | \n",
1842 | " barcelona | \n",
1843 | " messi | \n",
1844 | " 30.0 | \n",
1845 | "
\n",
1846 | " \n",
1847 | " 1 | \n",
1848 | " real_madrid | \n",
1849 | " ramos | \n",
1850 | " 20.0 | \n",
1851 | "
\n",
1852 | " \n",
1853 | "
\n",
1854 | "
"
1855 | ],
1856 | "text/plain": [
1857 | " Football_team Footballer money\n",
1858 | "0 barcelona messi 30.0\n",
1859 | "1 real_madrid ramos 20.0"
1860 | ]
1861 | },
1862 | "execution_count": 94,
1863 | "metadata": {},
1864 | "output_type": "execute_result"
1865 | }
1866 | ],
1867 | "source": [
1868 | "df"
1869 | ]
1870 | },
1871 | {
1872 | "cell_type": "code",
1873 | "execution_count": 95,
1874 | "metadata": {},
1875 | "outputs": [
1876 | {
1877 | "data": {
1878 | "text/html": [
1879 | "\n",
1880 | "\n",
1893 | "
\n",
1894 | " \n",
1895 | " \n",
1896 | " | \n",
1897 | " Football_team | \n",
1898 | " Footballer | \n",
1899 | " money | \n",
1900 | "
\n",
1901 | " \n",
1902 | " \n",
1903 | " \n",
1904 | " 0 | \n",
1905 | " barcelonabarcelona | \n",
1906 | " messimessi | \n",
1907 | " 60.0 | \n",
1908 | "
\n",
1909 | " \n",
1910 | " 1 | \n",
1911 | " real_madridreal_madrid | \n",
1912 | " ramosramos | \n",
1913 | " 40.0 | \n",
1914 | "
\n",
1915 | " \n",
1916 | "
\n",
1917 | "
"
1918 | ],
1919 | "text/plain": [
1920 | " Football_team Footballer money\n",
1921 | "0 barcelonabarcelona messimessi 60.0\n",
1922 | "1 real_madridreal_madrid ramosramos 40.0"
1923 | ]
1924 | },
1925 | "execution_count": 95,
1926 | "metadata": {},
1927 | "output_type": "execute_result"
1928 | }
1929 | ],
1930 | "source": [
1931 | "f = lambda x: x*2\n",
1932 | "df.apply(f)"
1933 | ]
1934 | },
1935 | {
1936 | "cell_type": "code",
1937 | "execution_count": 97,
1938 | "metadata": {},
1939 | "outputs": [
1940 | {
1941 | "data": {
1942 | "text/html": [
1943 | "\n",
1944 | "\n",
1957 | "
\n",
1958 | " \n",
1959 | " \n",
1960 | " | \n",
1961 | " Football_team | \n",
1962 | " Footballer | \n",
1963 | " money | \n",
1964 | "
\n",
1965 | " \n",
1966 | " \n",
1967 | " \n",
1968 | " 0 | \n",
1969 | " barcelonabarcelona | \n",
1970 | " messimessi | \n",
1971 | " 60.0 | \n",
1972 | "
\n",
1973 | " \n",
1974 | " 1 | \n",
1975 | " real_madridreal_madrid | \n",
1976 | " ramosramos | \n",
1977 | " 40.0 | \n",
1978 | "
\n",
1979 | " \n",
1980 | "
\n",
1981 | "
"
1982 | ],
1983 | "text/plain": [
1984 | " Football_team Footballer money\n",
1985 | "0 barcelonabarcelona messimessi 60.0\n",
1986 | "1 real_madridreal_madrid ramosramos 40.0"
1987 | ]
1988 | },
1989 | "execution_count": 97,
1990 | "metadata": {},
1991 | "output_type": "execute_result"
1992 | }
1993 | ],
1994 | "source": [
1995 | "df.applymap(f)"
1996 | ]
1997 | },
1998 | {
1999 | "cell_type": "markdown",
2000 | "metadata": {},
2001 | "source": [
2002 | "\n",
2003 | "## Data Alignment\n",
2004 | ""
2005 | ]
2006 | },
2007 | {
2008 | "cell_type": "code",
2009 | "execution_count": 124,
2010 | "metadata": {},
2011 | "outputs": [
2012 | {
2013 | "data": {
2014 | "text/plain": [
2015 | "c1 10\n",
2016 | "c2 20\n",
2017 | "c3 30\n",
2018 | "dtype: int64"
2019 | ]
2020 | },
2021 | "execution_count": 124,
2022 | "metadata": {},
2023 | "output_type": "execute_result"
2024 | }
2025 | ],
2026 | "source": [
2027 | "series = pd.Series([10,20,30], index = [\"c1\",\"c2\",\"c3\"])\n",
2028 | "series"
2029 | ]
2030 | },
2031 | {
2032 | "cell_type": "code",
2033 | "execution_count": 125,
2034 | "metadata": {},
2035 | "outputs": [
2036 | {
2037 | "data": {
2038 | "text/plain": [
2039 | "c1 1\n",
2040 | "c2 2\n",
2041 | "dtype: int64"
2042 | ]
2043 | },
2044 | "execution_count": 125,
2045 | "metadata": {},
2046 | "output_type": "execute_result"
2047 | }
2048 | ],
2049 | "source": [
2050 | "s = pd.Series([1,2], index = [\"c1\",\"c2\"])\n",
2051 | "s\n"
2052 | ]
2053 | },
2054 | {
2055 | "cell_type": "code",
2056 | "execution_count": 126,
2057 | "metadata": {},
2058 | "outputs": [
2059 | {
2060 | "data": {
2061 | "text/plain": [
2062 | "c1 11.0\n",
2063 | "c2 22.0\n",
2064 | "c3 NaN\n",
2065 | "dtype: float64"
2066 | ]
2067 | },
2068 | "execution_count": 126,
2069 | "metadata": {},
2070 | "output_type": "execute_result"
2071 | }
2072 | ],
2073 | "source": [
2074 | "series + s"
2075 | ]
2076 | },
2077 | {
2078 | "cell_type": "code",
2079 | "execution_count": 128,
2080 | "metadata": {},
2081 | "outputs": [
2082 | {
2083 | "data": {
2084 | "text/plain": [
2085 | "c1 11.0\n",
2086 | "c2 22.0\n",
2087 | "c3 30.0\n",
2088 | "dtype: float64"
2089 | ]
2090 | },
2091 | "execution_count": 128,
2092 | "metadata": {},
2093 | "output_type": "execute_result"
2094 | }
2095 | ],
2096 | "source": [
2097 | "s.add(series,fill_value=0)"
2098 | ]
2099 | },
2100 | {
2101 | "cell_type": "code",
2102 | "execution_count": null,
2103 | "metadata": {},
2104 | "outputs": [],
2105 | "source": []
2106 | }
2107 | ],
2108 | "metadata": {
2109 | "kernelspec": {
2110 | "display_name": "Python 3",
2111 | "language": "python",
2112 | "name": "python3"
2113 | },
2114 | "language_info": {
2115 | "codemirror_mode": {
2116 | "name": "ipython",
2117 | "version": 3
2118 | },
2119 | "file_extension": ".py",
2120 | "mimetype": "text/x-python",
2121 | "name": "python",
2122 | "nbconvert_exporter": "python",
2123 | "pygments_lexer": "ipython3",
2124 | "version": "3.7.1"
2125 | }
2126 | },
2127 | "nbformat": 4,
2128 | "nbformat_minor": 2
2129 | }
2130 |
--------------------------------------------------------------------------------
/3) Pandas/Pandas.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dataiteam/Data-Science-Cheat-Sheet/2ee8d83062b676ceb055865c688d9c5ebf7e3deb/3) Pandas/Pandas.pdf
--------------------------------------------------------------------------------
/3) Pandas/first.csv:
--------------------------------------------------------------------------------
1 | ,Football_team,Footballer,money
2 | 0,barcelona,messi,30.0
3 | 1,real_madrid,ramos,20.0
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Data-Science-Cheat-Sheet
2 | Youtube Data Science Cheat Sheet Course
3 |
--------------------------------------------------------------------------------