{"cells":[{"cell_type":"markdown","metadata":{"id":"bWvheXKXh1cZ"},"source":["# 3 Practice and Comprehensive Questions Solutions"]},{"cell_type":"code","execution_count":62,"metadata":{"id":"5GkVPPRZh1cZ","colab":{"base_uri":"https://localhost:8080/","height":35},"executionInfo":{"status":"ok","timestamp":1790160546541,"user_tz":-540,"elapsed":18,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"2a2d5e7c-6339-422b-a4d3-6a30bf126293"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["'%.3f'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":62}],"source":["# The following libraries are used, so please load them beforehand\n","import numpy as np\n","import numpy.random as random\n","import scipy as sp\n","import pandas as pd\n","from pandas import Series,DataFrame\n","import os\n","import requests, zipfile\n","from io import StringIO\n","import io\n","\n","# Visualization Library\n","import matplotlib.pyplot as plt\n","import matplotlib as mpl\n","import seaborn as sns\n","%matplotlib inline\n","\n","# Displayed to 3 decimal places\n","%precision 3"]},{"cell_type":"code","source":["# Specify the url with data\n","url = 'https://github.com/Hernan4444/MyAnimeList-Database/archive/refs/heads/master.zip'\n","\n","# Acquire data from the url\n","r = requests.get(url, stream=True)\n","\n","# read and extract the zipfile\n","z = zipfile.ZipFile(io.BytesIO(r.content))\n","z.extractall()"],"metadata":{"id":"j2HcbqvktDYM","executionInfo":{"status":"ok","timestamp":1790160548091,"user_tz":-540,"elapsed":1259,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"execution_count":64,"outputs":[]},{"cell_type":"code","source":["!sudo apt-get install tree\n","!tree MyAnimeList-Database-master/"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rX44U3c1tF14","executionInfo":{"status":"ok","timestamp":1790160551440,"user_tz":-540,"elapsed":3347,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"376867d8-0629-4000-dac6-3231f378c344"},"execution_count":65,"outputs":[{"output_type":"stream","name":"stdout","text":["Reading package lists... Done\n","Building dependency tree... Done\n","Reading state information... Done\n","tree is already the newest version (2.1.1-2ubuntu3.24.04.2).\n","0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n","\u001b[01;34mMyAnimeList-Database-master/\u001b[0m\n","├── \u001b[00mAll Scrapping process.ipynb\u001b[0m\n","├── \u001b[01;34mdata\u001b[0m\n","│   ├── \u001b[00manime.csv\u001b[0m\n","│   ├── \u001b[00manimelist.csv\u001b[0m\n","│   ├── \u001b[00manime_with_synopsis.csv\u001b[0m\n","│   ├── \u001b[01;34mhtml\u001b[0m\n","│   │   ├── \u001b[01;31m1.zip\u001b[0m\n","│   │   └── \u001b[01;31m5.zip\u001b[0m\n","│   ├── \u001b[00mrating_complete.csv\u001b[0m\n","│   ├── \u001b[00mreadme.md\u001b[0m\n","│   └── \u001b[00mwatching_status.csv\u001b[0m\n","└── \u001b[00mreadme.md\u001b[0m\n","\n","3 directories, 10 files\n"]}]},{"cell_type":"code","source":["anime_data = pd.read_csv('MyAnimeList-Database-master/data/anime.csv')\n","anime_list = pd.read_csv('MyAnimeList-Database-master/data/animelist.csv')\n","anime_synop = pd.read_csv('MyAnimeList-Database-master/data/anime_with_synopsis.csv')\n","\n","cols_to_use = [\n"," 'MAL_ID', 'Name', 'Score', 'Genres', 'Type', 'Aired', 'Studios', 'Source','Members'\n","]\n","anime_data_extracted = anime_data[cols_to_use]\n","anime_data_extracted.head()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"xAlJq2q6tJCB","executionInfo":{"status":"ok","timestamp":1790160551878,"user_tz":-540,"elapsed":436,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"aac8be82-0109-4db5-87aa-f32b17347d4f"},"execution_count":66,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" MAL_ID Name Score \\\n","0 1 Cowboy Bebop 8.78 \n","1 5 Cowboy Bebop: Tengoku no Tobira 8.39 \n","2 6 Trigun 8.24 \n","3 7 Witch Hunter Robin 7.27 \n","4 8 Bouken Ou Beet 6.98 \n","\n"," Genres Type \\\n","0 Action, Adventure, Comedy, Drama, Sci-Fi, Space TV \n","1 Action, Drama, Mystery, Sci-Fi, Space Movie \n","2 Action, Sci-Fi, Adventure, Comedy, Drama, Shounen TV \n","3 Action, Mystery, Police, Supernatural, Drama, ... TV \n","4 Adventure, Fantasy, Shounen, Supernatural TV \n","\n"," Aired Studios Source Members \n","0 Apr 3, 1998 to Apr 24, 1999 Sunrise Original 1251960 \n","1 Sep 1, 2001 Bones Original 273145 \n","2 Apr 1, 1998 to Sep 30, 1998 Madhouse Manga 558913 \n","3 Jul 2, 2002 to Dec 24, 2002 Sunrise Original 94683 \n","4 Sep 30, 2004 to Sep 29, 2005 Toei Animation Manga 13224 "],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
MAL_IDNameScoreGenresTypeAiredStudiosSourceMembers
01Cowboy Bebop8.78Action, Adventure, Comedy, Drama, Sci-Fi, SpaceTVApr 3, 1998 to Apr 24, 1999SunriseOriginal1251960
15Cowboy Bebop: Tengoku no Tobira8.39Action, Drama, Mystery, Sci-Fi, SpaceMovieSep 1, 2001BonesOriginal273145
26Trigun8.24Action, Sci-Fi, Adventure, Comedy, Drama, ShounenTVApr 1, 1998 to Sep 30, 1998MadhouseManga558913
37Witch Hunter Robin7.27Action, Mystery, Police, Supernatural, Drama, ...TVJul 2, 2002 to Dec 24, 2002SunriseOriginal94683
48Bouken Ou Beet6.98Adventure, Fantasy, Shounen, SupernaturalTVSep 30, 2004 to Sep 29, 2005Toei AnimationManga13224
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"anime_data_extracted","summary":"{\n \"name\": \"anime_data_extracted\",\n \"rows\": 17562,\n \"fields\": [\n {\n \"column\": \"MAL_ID\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14900,\n \"min\": 1,\n \"max\": 48492,\n \"num_unique_values\": 17562,\n \"samples\": [\n 40176,\n 13969,\n 13459\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 17558,\n \"samples\": [\n \"Soukou Kijo Iris Special\",\n \"Sugar Bunnies Fleur\",\n \"Code-E\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Score\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 533,\n \"samples\": [\n \"8.06\",\n \"3.72\",\n \"6.48\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Genres\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5034,\n \"samples\": [\n \"Action, Horror, Supernatural, Drama, Fantasy\",\n \"Game, Adventure, Shounen\",\n \"Music, Drama, Fantasy\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Type\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 7,\n \"samples\": [\n \"TV\",\n \"Movie\",\n \"Music\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Aired\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 11947,\n \"samples\": [\n \"Jan 18, 2001 to May 1, 2001\",\n \"Aug 15, 2019\",\n \"Apr 3, 2006 to Feb 6, 2009\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Studios\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1090,\n \"samples\": [\n \"Directions\",\n \"Sunrise Beyond\",\n \"10Gauge, Studio DURIAN\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Source\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 16,\n \"samples\": [\n \"Original\",\n \"Manga\",\n \"4-koma manga\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Members\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 125282,\n \"min\": 1,\n \"max\": 2589552,\n \"num_unique_values\": 9508,\n \"samples\": [\n 26126,\n 104734,\n 11477\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":66}]},{"cell_type":"markdown","metadata":{"id":"rC_px-Euh1ca"},"source":["#### \n","\n","From `anime_data_extracted` that we just created, extract data where `Source` is `Manga` **and** `Members` is above 100000."]},{"cell_type":"code","execution_count":67,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"executionInfo":{"elapsed":34,"status":"ok","timestamp":1790160551914,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"t5XzMnsVh1cb","outputId":"06eff044-16bc-4ac5-942a-5d1e36e51073"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" MAL_ID Name Score \\\n","2 6 Trigun 8.24 \n","5 15 Eyeshield 21 7.95 \n","6 16 Hachimitsu to Clover 8.06 \n","8 18 Initial D Fourth Stage 8.15 \n","9 19 Monster 8.76 \n","\n"," Genres Type \\\n","2 Action, Sci-Fi, Adventure, Comedy, Drama, Shounen TV \n","5 Action, Sports, Comedy, Shounen TV \n","6 Comedy, Drama, Josei, Romance, Slice of Life TV \n","8 Action, Cars, Sports, Drama, Seinen TV \n","9 Drama, Horror, Mystery, Police, Psychological,... TV \n","\n"," Aired Studios Source Members \n","2 Apr 1, 1998 to Sep 30, 1998 Madhouse Manga 558913 \n","5 Apr 6, 2005 to Mar 19, 2008 Gallop Manga 148259 \n","6 Apr 15, 2005 to Sep 27, 2005 J.C.Staff Manga 214499 \n","8 Apr 17, 2004 to Feb 18, 2006 A.C.G.T. Manga 117929 \n","9 Apr 7, 2004 to Sep 28, 2005 Madhouse Manga 614100 "],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
MAL_IDNameScoreGenresTypeAiredStudiosSourceMembers
26Trigun8.24Action, Sci-Fi, Adventure, Comedy, Drama, ShounenTVApr 1, 1998 to Sep 30, 1998MadhouseManga558913
515Eyeshield 217.95Action, Sports, Comedy, ShounenTVApr 6, 2005 to Mar 19, 2008GallopManga148259
616Hachimitsu to Clover8.06Comedy, Drama, Josei, Romance, Slice of LifeTVApr 15, 2005 to Sep 27, 2005J.C.StaffManga214499
818Initial D Fourth Stage8.15Action, Cars, Sports, Drama, SeinenTVApr 17, 2004 to Feb 18, 2006A.C.G.T.Manga117929
919Monster8.76Drama, Horror, Mystery, Police, Psychological,...TVApr 7, 2004 to Sep 28, 2005MadhouseManga614100
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"anime_data_extracted[(anime_data_extracted['Source'] == 'Manga') & (anime_data_extracted['Members'] > 100000)]\",\n \"rows\": 5,\n \"fields\": [\n {\n \"column\": \"MAL_ID\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 5,\n \"min\": 6,\n \"max\": 19,\n \"num_unique_values\": 5,\n \"samples\": [\n 15,\n 19,\n 16\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Eyeshield 21\",\n \"Monster\",\n \"Hachimitsu to Clover\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Score\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"7.95\",\n \"8.76\",\n \"8.06\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Genres\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Action, Sports, Comedy, Shounen\",\n \"Drama, Horror, Mystery, Police, Psychological, Seinen, Thriller\",\n \"Comedy, Drama, Josei, Romance, Slice of Life\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Type\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"TV\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Aired\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Apr 6, 2005 to Mar 19, 2008\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Studios\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"Gallop\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Source\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"Manga\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Members\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 236883,\n \"min\": 117929,\n \"max\": 614100,\n \"num_unique_values\": 5,\n \"samples\": [\n 148259\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":67}],"source":["# Condition filtering\n","anime_data_extracted = anime_data_extracted[anime_data_extracted['Score'] != 'Unknown']\n","\n","anime_data_extracted[(anime_data_extracted['Source'] == 'Manga') & (anime_data_extracted['Members'] > 100000)].head()"]},{"cell_type":"markdown","metadata":{"id":"vUqB7BzW5ixI"},"source":["#### \n","\n","Using only the `sort_values()` and `head()` functions, find the names of the 10 most recently added anime in this dataset.\n","\n","Hint: Notice that `MAL_ID` values are larger for anime that were added recently, so sorting them by descending order will sort the anime from the most recent to the oldest."]},{"cell_type":"code","execution_count":68,"metadata":{"id":"9UF0O7dG5j3T","colab":{"base_uri":"https://localhost:8080/","height":363},"executionInfo":{"status":"ok","timestamp":1790160551950,"user_tz":-540,"elapsed":35,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"035b74b5-0e7c-43fa-a8ba-eca41c160653"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" MAL_ID Name Score \\\n","17552 48456 SK∞: Crazy Rock Jam 6.52 \n","17513 47616 Yakusoku no Neverland 2nd Season: Michishirube 4.81 \n","17512 47614 Nu Wushen de Canzhuo Spring Festival Special 6.83 \n","17505 47402 Heikousen 7.52 \n","17504 47398 Kimetsu Gakuen: Valentine-hen 6.59 \n","17502 47307 Shin Gengou Nyankogelion 6.41 \n","17478 46491 Santa Company: Manatsu no Merry Christmas 5.68 \n","17469 46118 Wave!!: Surfing Yappe!! (TV) 6.05 \n","17462 45999 Enen no Shouboutai Mini Anime 5.92 \n","17459 45782 D4DJ Petit Mix 6.51 \n","\n"," Genres Type \\\n","17552 Comedy, Sports Special \n","17513 Mystery, Psychological, Supernatural, Thriller... Special \n","17512 Slice of Life, Comedy Special \n","17505 Music, Romance Music \n","17504 Comedy ONA \n","17502 Sci-Fi, Music, Mecha Music \n","17478 Fantasy Movie \n","17469 Slice of Life, Sports TV \n","17462 Comedy ONA \n","17459 Music, Comedy TV \n","\n"," Aired Studios Source Members \n","17552 Mar 14, 2021 Bones Original 10722 \n","17513 Feb 12, 2021 CloverWorks Manga 13070 \n","17512 Feb 11, 2021 Unknown Game 540 \n","17505 Feb 5, 2021 10Gauge, Studio DURIAN Original 832 \n","17504 Feb 14, 2021 Unknown Original 18161 \n","17502 Jan 22, 2021 PONOS Corporation Original 472 \n","17478 Jan 28, 2021 ORENDA Original 1677 \n","17469 Jan 12, 2021 to ? Asahi Production Other 19952 \n","17462 Jan 8, 2021 to ? Unknown Manga 5392 \n","17459 Feb 5, 2021 to ? W-Toon Studio, DMM.futureworks Original 1575 "],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
MAL_IDNameScoreGenresTypeAiredStudiosSourceMembers
1755248456SK∞: Crazy Rock Jam6.52Comedy, SportsSpecialMar 14, 2021BonesOriginal10722
1751347616Yakusoku no Neverland 2nd Season: Michishirube4.81Mystery, Psychological, Supernatural, Thriller...SpecialFeb 12, 2021CloverWorksManga13070
1751247614Nu Wushen de Canzhuo Spring Festival Special6.83Slice of Life, ComedySpecialFeb 11, 2021UnknownGame540
1750547402Heikousen7.52Music, RomanceMusicFeb 5, 202110Gauge, Studio DURIANOriginal832
1750447398Kimetsu Gakuen: Valentine-hen6.59ComedyONAFeb 14, 2021UnknownOriginal18161
1750247307Shin Gengou Nyankogelion6.41Sci-Fi, Music, MechaMusicJan 22, 2021PONOS CorporationOriginal472
1747846491Santa Company: Manatsu no Merry Christmas5.68FantasyMovieJan 28, 2021ORENDAOriginal1677
1746946118Wave!!: Surfing Yappe!! (TV)6.05Slice of Life, SportsTVJan 12, 2021 to ?Asahi ProductionOther19952
1746245999Enen no Shouboutai Mini Anime5.92ComedyONAJan 8, 2021 to ?UnknownManga5392
1745945782D4DJ Petit Mix6.51Music, ComedyTVFeb 5, 2021 to ?W-Toon Studio, DMM.futureworksOriginal1575
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"anime_data_extracted\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"MAL_ID\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 869,\n \"min\": 45782,\n \"max\": 48456,\n \"num_unique_values\": 10,\n \"samples\": [\n 45999,\n 47616,\n 47307\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Enen no Shouboutai Mini Anime\",\n \"Yakusoku no Neverland 2nd Season: Michishirube\",\n \"Shin Gengou Nyankogelion\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Score\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"5.92\",\n \"4.81\",\n \"6.41\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Genres\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 9,\n \"samples\": [\n \"Slice of Life, Sports\",\n \"Mystery, Psychological, Supernatural, Thriller, Shounen\",\n \"Sci-Fi, Music, Mecha\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Type\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Music\",\n \"TV\",\n \"ONA\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Aired\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Jan 8, 2021 to ?\",\n \"Feb 12, 2021\",\n \"Jan 22, 2021\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Studios\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"CloverWorks\",\n \"ORENDA\",\n \"Bones\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Source\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"Manga\",\n \"Other\",\n \"Original\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Members\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 7639,\n \"min\": 472,\n \"max\": 19952,\n \"num_unique_values\": 10,\n \"samples\": [\n 5392,\n 13070,\n 472\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":68}],"source":["anime_data_extracted.sort_values(by='MAL_ID',ascending=False).head(10)"]},{"cell_type":"markdown","metadata":{"id":"pPTiRFPjh1cc"},"source":["#### \n","\n","Let's do an internal join between the following two data tables with the same `id` value."]},{"cell_type":"code","execution_count":69,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":45,"status":"ok","timestamp":1790160551996,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"xU_ud5ieh1cc","outputId":"5e202eff-5a81-4e6b-bfb1-4c85e195ba68"},"outputs":[{"output_type":"stream","name":"stdout","text":[" id city birth_year name\n","0 0 Tokyo 1990 Hiroshi\n","1 1 Osaka 1989 Akiko\n","2 2 Kyoto 1992 Yuki\n","3 3 Hokkaido 1997 Satoru\n","4 4 Tokyo 1982 Steeve\n","5 6 Tokyo 1991 Mituru\n","6 8 Osaka 1988 Aoi\n","7 11 Kyoto 1990 Tarou\n","8 12 Hokkaido 1995 Suguru\n","9 13 Tokyo 1981 Mitsuo\n"," id math English sex index_num\n","0 0 20 30 M 0\n","1 1 30 50 F 1\n","2 3 50 50 F 2\n","3 6 70 70 M 3\n","4 8 90 20 M 4\n"]}],"source":["# Prepare data 4\n","data4 = {\n"," 'id':['0','1','2','3','4','6','8','11','12','13'],\n"," 'city':['Tokyo','Osaka','Kyoto','Hokkaido','Tokyo','Tokyo','Osaka','Kyoto','Hokkaido','Tokyo'],\n"," 'birth_year':[1990,1989,1992,1997,1982,1991,1988,1990,1995,1981],\n"," 'name':['Hiroshi','Akiko','Yuki','Satoru','Steeve','Mituru','Aoi','Tarou','Suguru','Mitsuo']\n","}\n","df4 = DataFrame(data4)\n","print(df4)\n","\n","# Prepare data 5\n","data5 = {\n"," 'id':['0','1','3','6','8'],\n"," 'math':[20,30,50,70,90],\n"," 'English':[30,50,50,70,20],\n"," 'sex':['M','F','F','M','M'],\n"," 'index_num':[0,1,2,3,4]\n","}\n","df5 = DataFrame(data5)\n","print(df5)"]},{"cell_type":"code","execution_count":70,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"executionInfo":{"elapsed":25,"status":"ok","timestamp":1790160552015,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"9A6tJO6rh1cd","outputId":"5691be8b-54a3-44d4-a6f5-0ddcd558fdf3"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" id city birth_year name math English sex index_num\n","0 0 Tokyo 1990 Hiroshi 20 30 M 0\n","1 1 Osaka 1989 Akiko 30 50 F 1\n","2 3 Hokkaido 1997 Satoru 50 50 F 2\n","3 6 Tokyo 1991 Mituru 70 70 M 3\n","4 8 Osaka 1988 Aoi 90 20 M 4"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
idcitybirth_yearnamemathEnglishsexindex_num
00Tokyo1990Hiroshi2030M0
11Osaka1989Akiko3050F1
23Hokkaido1997Satoru5050F2
36Tokyo1991Mituru7070M3
48Osaka1988Aoi9020M4
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"pd\",\n \"rows\": 5,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"1\",\n \"8\",\n \"3\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"city\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Tokyo\",\n \"Osaka\",\n \"Hokkaido\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"birth_year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3,\n \"min\": 1988,\n \"max\": 1997,\n \"num_unique_values\": 5,\n \"samples\": [\n 1989,\n 1988,\n 1997\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Akiko\",\n \"Aoi\",\n \"Satoru\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"math\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 28,\n \"min\": 20,\n \"max\": 90,\n \"num_unique_values\": 5,\n \"samples\": [\n 30,\n 90,\n 50\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"English\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 19,\n \"min\": 20,\n \"max\": 70,\n \"num_unique_values\": 4,\n \"samples\": [\n 50,\n 20,\n 30\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"F\",\n \"M\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"index_num\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 4,\n \"num_unique_values\": 5,\n \"samples\": [\n 1,\n 4\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":70}],"source":["# Answer\n","pd.merge(df4, df5, on='id')"]},{"cell_type":"markdown","metadata":{"id":"GZC_TYeIh1ce"},"source":["#### \n","Using the data from , let's vertically join the following data for `df4`."]},{"cell_type":"code","execution_count":71,"metadata":{"id":"XynJBATUh1ce","executionInfo":{"status":"ok","timestamp":1790160552070,"user_tz":-540,"elapsed":36,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# Preparing data\n","data6 = {\n"," 'id':['70','80','90','120','150'],\n"," 'city':['Chiba','Kanagawa','Tokyo','Fukuoka','Okinawa'],\n"," 'birth_year':[1980,1999,1995,1994,1994],\n"," 'name':['Suguru','Kouichi','Satochi','Yukie','Akari']\n","}\n","df6 = DataFrame(data6)"]},{"cell_type":"code","execution_count":72,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":520},"executionInfo":{"elapsed":4,"status":"ok","timestamp":1790160552076,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"QGgJQf4zh1cf","outputId":"7d3c8d0b-7594-42c4-d03d-d5edb48901a6"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" id city birth_year name\n","0 0 Tokyo 1990 Hiroshi\n","1 1 Osaka 1989 Akiko\n","2 2 Kyoto 1992 Yuki\n","3 3 Hokkaido 1997 Satoru\n","4 4 Tokyo 1982 Steeve\n","5 6 Tokyo 1991 Mituru\n","6 8 Osaka 1988 Aoi\n","7 11 Kyoto 1990 Tarou\n","8 12 Hokkaido 1995 Suguru\n","9 13 Tokyo 1981 Mitsuo\n","0 70 Chiba 1980 Suguru\n","1 80 Kanagawa 1999 Kouichi\n","2 90 Tokyo 1995 Satochi\n","3 120 Fukuoka 1994 Yukie\n","4 150 Okinawa 1994 Akari"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
idcitybirth_yearname
00Tokyo1990Hiroshi
11Osaka1989Akiko
22Kyoto1992Yuki
33Hokkaido1997Satoru
44Tokyo1982Steeve
56Tokyo1991Mituru
68Osaka1988Aoi
711Kyoto1990Tarou
812Hokkaido1995Suguru
913Tokyo1981Mitsuo
070Chiba1980Suguru
180Kanagawa1999Kouichi
290Tokyo1995Satochi
3120Fukuoka1994Yukie
4150Okinawa1994Akari
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"pd\",\n \"rows\": 15,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 15,\n \"samples\": [\n \"13\",\n \"80\",\n \"0\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"city\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"Osaka\",\n \"Kanagawa\",\n \"Tokyo\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"birth_year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 5,\n \"min\": 1980,\n \"max\": 1999,\n \"num_unique_values\": 12,\n \"samples\": [\n 1999,\n 1980,\n 1990\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 14,\n \"samples\": [\n \"Mitsuo\",\n \"Satochi\",\n \"Hiroshi\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":72}],"source":["pd.concat([df4, df6])"]},{"cell_type":"code","execution_count":73,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"executionInfo":{"elapsed":4,"status":"ok","timestamp":1790160552081,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"OPqJBCCch1cf","outputId":"9faddd82-3b80-4fdc-ae43-7b090da219c9"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["city Kyoto Nagoya Hokkaido Kyoto\n","color Yellow Yellow Red Blue\n","key1 key2 \n","c 1 0 1 2 3\n","d 2 4 5 6 7\n"," 1 8 9 10 11"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
cityKyotoNagoyaHokkaidoKyoto
colorYellowYellowRedBlue
key1key2
c10123
d24567
1891011
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"hier_df1","summary":"{\n \"name\": \"hier_df1\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": [\n \"Kyoto\",\n \"Yellow\"\n ],\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 4,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 3,\n \"samples\": [\n 0,\n 4,\n 8\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": [\n \"Nagoya\",\n \"Yellow\"\n ],\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 4,\n \"min\": 1,\n \"max\": 9,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 5,\n 9\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": [\n \"Hokkaido\",\n \"Red\"\n ],\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 4,\n \"min\": 2,\n \"max\": 10,\n \"num_unique_values\": 3,\n \"samples\": [\n 2,\n 6,\n 10\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": [\n \"Kyoto\",\n \"Blue\"\n ],\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 4,\n \"min\": 3,\n \"max\": 11,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 7,\n 11\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":73}],"source":["hier_df1 = DataFrame(\n"," np.arange(12).reshape((3,4)),\n"," index = [['c','d','d'], [1,2,1]],\n"," columns = [\n"," ['Kyoto','Nagoya','Hokkaido','Kyoto'],\n"," ['Yellow','Yellow','Red','Blue']\n"," ]\n",")\n","\n","hier_df1.index.names = ['key1','key2']\n","hier_df1.columns.names = ['city','color']\n","hier_df1"]},{"cell_type":"markdown","metadata":{"id":"t0owRdFQh1ch"},"source":["#### \n","\n","Let's examine the relationship between `Score` and `Members` in the `anime_data_extracted` dataset.\n","\n","(1) Group `Scores` into fixed rating ranges and save the result as `score_tiers`:\n","1. 0.0~6.0: 'Low'\n","2. 6.0~7.5: 'Average'\n","3. 7.5~8.5: 'High'\n","4. 8.5~10.0: 'Very High'"]},{"cell_type":"code","execution_count":74,"metadata":{"id":"LL0Ubi7Ih1ch","executionInfo":{"status":"ok","timestamp":1790160552129,"user_tz":-540,"elapsed":46,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# Convert the Score column to type float first\n","anime_data_extracted[\"Score\"] = anime_data_extracted[\"Score\"].astype(float)\n","\n","scores = [0.0,6.0,7.5,8.5,10.0]\n","score_tier_names = ['Low','Average','High','Very High']\n","score_tiers = pd.cut(anime_data_extracted[\"Score\"], scores, labels=score_tier_names)"]},{"cell_type":"markdown","source":["(2) Divide the member counts into 4 equal-sized popularity groups and save the result as `popularity_tiers`. Use the names 'Niche', 'Moderate', 'Popular', and 'Viral Hit' from lowest to highers."],"metadata":{"id":"que8FkzMw8Hb"}},{"cell_type":"code","source":["popularity_tier_names = ['Niche','Moderate','Popular','Viral Hit']\n","popularity_tiers = pd.qcut(anime_data_extracted['Members'],4,labels=popularity_tier_names)"],"metadata":{"id":"6e1EvLjjw-HH","executionInfo":{"status":"ok","timestamp":1790160552133,"user_tz":-540,"elapsed":2,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"execution_count":75,"outputs":[]},{"cell_type":"markdown","source":["Run this code to display a 4 by 4 frequency grid showing how many anime fall into each score and popularity combination."],"metadata":{"id":"fyqrTI8wxoN8"}},{"cell_type":"code","source":["# Turns row categories into a full 2D table\n","pd.DataFrame({\"Score\": score_tiers, \"Members\": popularity_tiers}).value_counts().unstack()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"crzSkv2vxq5p","executionInfo":{"status":"ok","timestamp":1790160552195,"user_tz":-540,"elapsed":60,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"16ad21d9-7576-45b7-d8ce-4f67698e9945"},"execution_count":76,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Members Niche Moderate Popular Viral Hit\n","Score \n","Low 2000.0 941.0 352.0 100.0\n","Average 1108.0 2131.0 2446.0 1770.0\n","High 2.0 29.0 305.0 1134.0\n","Very High NaN NaN 2.0 101.0"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
MembersNicheModeratePopularViral Hit
Score
Low2000.0941.0352.0100.0
Average1108.02131.02446.01770.0
High2.029.0305.01134.0
Very HighNaNNaN2.0101.0
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"pd\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"Score\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"Average\",\n \"Very High\",\n \"Low\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Niche\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1000.9082542038173,\n \"min\": 2.0,\n \"max\": 2000.0,\n \"num_unique_values\": 3,\n \"samples\": [\n 2000.0,\n 1108.0,\n 2.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Moderate\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1054.0594543636205,\n \"min\": 29.0,\n \"max\": 2131.0,\n \"num_unique_values\": 3,\n \"samples\": [\n 941.0,\n 2131.0,\n 29.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Popular\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1123.9206303531105,\n \"min\": 2.0,\n \"max\": 2446.0,\n \"num_unique_values\": 4,\n \"samples\": [\n 2446.0,\n 2.0,\n 352.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Viral Hit\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 822.3544957417492,\n \"min\": 100.0,\n \"max\": 1770.0,\n \"num_unique_values\": 4,\n \"samples\": [\n 1770.0,\n 101.0,\n 100.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":76}]},{"cell_type":"markdown","metadata":{"id":"BvKWf-oxh1cj"},"source":["#### \n","\n","Anime fans often joke that shows adapted from \"Light Novels\" have ridiculously long titles compared to original shows or manga. Let's see if this is true with `anime_data_extracted`. Group the dataset by `Source` and calculate the average (mean) `Title_Length` for each source type. Sort the results in descending order to see which source has the largest names."]},{"cell_type":"code","execution_count":77,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":617},"executionInfo":{"elapsed":33,"status":"ok","timestamp":1790160552195,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"zj06plASh1cj","outputId":"96e52681-120a-4d19-981e-0cfff3b17747"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["Source\n","4-koma manga 25.959839\n","Book 23.294118\n","Card game 28.116667\n","Digital manga 34.076923\n","Game 29.982069\n","Light novel 32.557123\n","Manga 25.845590\n","Music 17.157635\n","Novel 24.204489\n","Original 23.118391\n","Other 24.910543\n","Picture book 19.941176\n","Radio 34.166667\n","Unknown 27.155207\n","Visual novel 26.676230\n","Web manga 24.973684\n","Name: Title_Length, dtype: float64"],"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
Title_Length
Source
4-koma manga25.959839
Book23.294118
Card game28.116667
Digital manga34.076923
Game29.982069
Light novel32.557123
Manga25.845590
Music17.157635
Novel24.204489
Original23.118391
Other24.910543
Picture book19.941176
Radio34.166667
Unknown27.155207
Visual novel26.676230
Web manga24.973684
\n","

"]},"metadata":{},"execution_count":77}],"source":["# Creates a new column named 'Title_Length' that will store the length of each anime\n","anime_data_extracted['Title_Length'] = anime_data_extracted['Name'].str.len()\n","\n","anime_data_extracted.groupby('Source')['Title_Length'].mean()"]},{"cell_type":"markdown","metadata":{"id":"YyBz0o72h1cj"},"source":["#### \n","\n","Let's investigate the different animation studios. Group the `anime_data_extracted` dataset by `Studios`, then use the `agg()` function on the `Score` column to find the `mean`, `max`, and `count` for each studio."]},{"cell_type":"code","execution_count":78,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":455},"executionInfo":{"elapsed":33,"status":"ok","timestamp":1790160552224,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"2SsezzPyh1cj","outputId":"eb79295e-7f41-44b8-fa36-b040d43c7124"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" mean max count\n","Studios \n","10Gauge 6.490000 6.53 2\n","10Gauge, Studio DURIAN 7.520000 7.52 1\n","2:10 AM Animation 7.023333 7.25 3\n","8bit 7.072683 8.07 41\n","A-1 Pictures 7.138703 8.74 185\n","... ... ... ...\n","monofilmo 7.335000 7.40 2\n","pH Studio 6.260000 6.26 1\n","production doA 7.336667 7.79 3\n","ufotable 7.248852 8.79 61\n","ufotable, feel., Studio Flag 6.910000 6.91 1\n","\n","[948 rows x 3 columns]"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
meanmaxcount
Studios
10Gauge6.4900006.532
10Gauge, Studio DURIAN7.5200007.521
2:10 AM Animation7.0233337.253
8bit7.0726838.0741
A-1 Pictures7.1387038.74185
............
monofilmo7.3350007.402
pH Studio6.2600006.261
production doA7.3366677.793
ufotable7.2488528.7961
ufotable, feel., Studio Flag6.9100006.911
\n","

948 rows × 3 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"anime_data_extracted\",\n \"rows\": 948,\n \"fields\": [\n {\n \"column\": \"Studios\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 948,\n \"samples\": [\n \"RAMS\",\n \"drop\",\n \"Bridge, Husio Studio\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"mean\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.7388706363199753,\n \"min\": 2.18,\n \"max\": 8.61,\n \"num_unique_values\": 621,\n \"samples\": [\n 6.845,\n 6.7884,\n 5.05\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"max\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.9573712794339168,\n \"min\": 2.18,\n \"max\": 9.19,\n \"num_unique_values\": 352,\n \"samples\": [\n 5.75,\n 8.61,\n 6.03\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 119,\n \"min\": 1,\n \"max\": 3511,\n \"num_unique_values\": 75,\n \"samples\": [\n 185,\n 442,\n 18\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":78}],"source":["anime_data_extracted.groupby('Studios')['Score'].agg(['mean','max','count'])"]},{"cell_type":"markdown","metadata":{"id":"ITHTpMFFh1ck"},"source":["#### \n","\n","We discussed many methods of handling missing data. Now let's observe how the choice of which method to use affects our analysis of the data. We are going to test these three methods on the `Score` column and see how it affects the mean value of `Score`.\n","\n","1. Drop all rows with missing data\n","2. Fill all missing `Score` data with -1\n","3. Fill all missing `Score` data with the mean value of the `Score` data we have\n","\n","Run this cell to turn all of the values in `Score` into `float` so we can calculate the mean.\n"]},{"cell_type":"code","source":["anime_data = anime_data.replace('Unknown', np.nan)\n","cols_to_use2 = [\n"," \"Name\", \"Score\", \"Type\", \"Premiered\", \"Studios\", \"Source\",\n","]\n","anime_data_extracted_pracq = anime_data[cols_to_use2]\n","anime_data_extracted_pracq[\"Score\"] = anime_data_extracted_pracq[\"Score\"].astype(float)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"aSjMCf492Ypf","executionInfo":{"status":"ok","timestamp":1790160552283,"user_tz":-540,"elapsed":32,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"de1018fd-63b5-47a6-9333-4b0e95f73872"},"execution_count":79,"outputs":[{"output_type":"stream","name":"stderr","text":["/tmp/ipykernel_3197/2207846101.py:6: SettingWithCopyWarning: \n","A value is trying to be set on a copy of a slice from a DataFrame.\n","Try using .loc[row_indexer,col_indexer] = value instead\n","\n","See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"," anime_data_extracted_pracq[\"Score\"] = anime_data_extracted_pracq[\"Score\"].astype(float)\n"]}]},{"cell_type":"markdown","source":["(1) Calculate the mean `Score` after removing all rows with at least one missing value."],"metadata":{"id":"kk9bf-z22wQt"}},{"cell_type":"code","source":["anime_data_extracted_dropna = anime_data_extracted_pracq.dropna()\n","anime_data_extracted_dropna[\"Score\"].mean()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Ym31yuYJ2y3R","executionInfo":{"status":"ok","timestamp":1790160552297,"user_tz":-540,"elapsed":12,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"5ef3f5d9-b459-43be-896a-1dfa5c304d3d"},"execution_count":80,"outputs":[{"output_type":"execute_result","data":{"text/plain":["np.float64(6.97429798578199)"]},"metadata":{},"execution_count":80}]},{"cell_type":"markdown","source":["(2) Calculate the mean `Score` after filling all missing values with -1."],"metadata":{"id":"_qZJbP2G20Gh"}},{"cell_type":"code","source":["anime_data_extracted_filln1 = anime_data_extracted_pracq.fillna(-1)\n","anime_data_extracted_filln1[\"Score\"].mean()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KJc90cqJ23gg","executionInfo":{"status":"ok","timestamp":1790160552331,"user_tz":-540,"elapsed":8,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"33c421aa-219f-49a8-9fc6-bf0fe4bcc216"},"execution_count":81,"outputs":[{"output_type":"execute_result","data":{"text/plain":["np.float64(4.311564742056714)"]},"metadata":{},"execution_count":81}]},{"cell_type":"markdown","source":["(3) Calculate the mean `Score` after filling all missing `Score` values with the mean `Score` value for the data we currently have."],"metadata":{"id":"nOgFI_rQ25vq"}},{"cell_type":"code","source":["anime_data_extracted_fillmean = anime_data_extracted_pracq.fillna(anime_data_extracted_pracq[\"Score\"].mean())\n","anime_data_extracted_fillmean[\"Score\"].mean()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"LuuSjgYC28nw","executionInfo":{"status":"ok","timestamp":1790160552343,"user_tz":-540,"elapsed":11,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"0a6e4919-e45b-47c6-9167-37a1e6ba2538"},"execution_count":82,"outputs":[{"output_type":"execute_result","data":{"text/plain":["np.float64(6.509999194911843)"]},"metadata":{},"execution_count":82}]},{"cell_type":"markdown","source":["As we can see, the method we choose to handle the missing values greatly affects the mean `Score` value. There is never an absolute correct method to choose, and it is your job as a data scientist to balance the trade offs from both the data science side and the domain side and to choose the method that makes the most sense."],"metadata":{"id":"TKqP_5k43EJp"}},{"cell_type":"markdown","metadata":{"id":"E8ZHfZo7h1co"},"source":["## 3.7 Comprehensive Question"]},{"cell_type":"markdown","metadata":{"id":"2Ym3VDDah1co"},"source":["### Comprehensive Question 3-1 Data Manipulation\n","\n","Using the data `student-mat.csv', which is the data showing math scores, answer the following questions.\n","\n","(1) For the above data, calculate the average score of `G1` by age (`age`) x sex (`sex`) and create a table (table) such that the vertical axis is age (`age`) and the horizontal axis is sex (`sex`).\n","\n","(2) For the result table displayed in (1), delete all the rows (records) that are NaN."]},{"cell_type":"code","source":["# Specify the url with data\n","url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/00320/student.zip'\n","\n","# Acquire data from the url\n","r = requests.get(url, stream=True)\n","\n","# read and extract the zipfile\n","z = zipfile.ZipFile(io.BytesIO(r.content))\n","z.extractall()\n","\n","# Load the data\n","student_data_math = pd.read_csv('student-mat.csv', sep=';')"],"metadata":{"id":"F6pXU3334Xf0","executionInfo":{"status":"ok","timestamp":1790160552544,"user_tz":-540,"elapsed":202,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"execution_count":83,"outputs":[]},{"cell_type":"code","execution_count":84,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":332},"executionInfo":{"elapsed":25,"status":"ok","timestamp":1790160552548,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"jGCjGRfGh1cp","outputId":"d82c9e72-99f4-4274-e4af-62b2d54eb843"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["sex F M\n","age \n","15 10.052632 12.250000\n","16 10.203704 11.740000\n","17 11.103448 10.600000\n","18 10.883721 10.538462\n","19 10.642857 9.700000\n","20 15.000000 13.000000\n","21 NaN 10.000000\n","22 NaN 6.000000"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
sexFM
age
1510.05263212.250000
1610.20370411.740000
1711.10344810.600000
1810.88372110.538462
1910.6428579.700000
2015.00000013.000000
21NaN10.000000
22NaN6.000000
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"student_data_math\",\n \"rows\": 8,\n \"fields\": [\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2,\n \"min\": 15,\n \"max\": 22,\n \"num_unique_values\": 8,\n \"samples\": [\n 16,\n 20,\n 15\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"F\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.8488000470985881,\n \"min\": 10.052631578947368,\n \"max\": 15.0,\n \"num_unique_values\": 6,\n \"samples\": [\n 10.052631578947368,\n 10.203703703703704,\n 15.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"M\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.1380198151382794,\n \"min\": 6.0,\n \"max\": 13.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 11.74,\n 13.0,\n 12.25\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":84}],"source":["# Answer\n","# (1)\n","student_data_math.groupby(['age','sex'])['G1'].mean().unstack()"]},{"cell_type":"code","execution_count":85,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":269},"executionInfo":{"elapsed":6,"status":"ok","timestamp":1790160552555,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"},"user_tz":-540},"id":"Fd85dOOhh1cp","outputId":"31f4342a-e2c0-48e8-9744-572dc21f5493","scrolled":true},"outputs":[{"output_type":"execute_result","data":{"text/plain":["sex F M\n","age \n","15 10.052632 12.250000\n","16 10.203704 11.740000\n","17 11.103448 10.600000\n","18 10.883721 10.538462\n","19 10.642857 9.700000\n","20 15.000000 13.000000"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
sexFM
age
1510.05263212.250000
1610.20370411.740000
1711.10344810.600000
1810.88372110.538462
1910.6428579.700000
2015.00000013.000000
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"student_data_math\",\n \"rows\": 6,\n \"fields\": [\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 15,\n \"max\": 20,\n \"num_unique_values\": 6,\n \"samples\": [\n 15,\n 16,\n 20\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"F\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.8488000470985881,\n \"min\": 10.052631578947368,\n \"max\": 15.0,\n \"num_unique_values\": 6,\n \"samples\": [\n 10.052631578947368,\n 10.203703703703704,\n 15.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"M\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.2341722585231316,\n \"min\": 9.7,\n \"max\": 13.0,\n \"num_unique_values\": 6,\n \"samples\": [\n 12.25,\n 11.74,\n 13.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":85}],"source":["# Answer\n","# (2)\n","student_data_math.groupby(['age','sex'])['G1'].mean().unstack().dropna()"]},{"cell_type":"markdown","metadata":{"id":"mA5_GWsWh1cp"},"source":["# How to Load Data"]},{"cell_type":"code","execution_count":86,"metadata":{"id":"sRQ2l5Tph1cp","executionInfo":{"status":"ok","timestamp":1790160553131,"user_tz":-540,"elapsed":552,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# Specify url with data\n","url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00320/student.zip'\n","# Specify data from url\n","r = requests.get(url, stream=True)\n","# Extract student-mat.csv in the zipfile to the data directory in the current directory\n","with zipfile.ZipFile(io.BytesIO(r.content)) as existing_zip:\n"," existing_zip.extract('student-mat.csv', 'data')\n","# Directory where csvfile exists\n","data_dir = './data'\n","path = os.path.join(data_dir, 'student-mat.csv')\n","student_data_math = pd.read_csv(path,sep=';')"]},{"cell_type":"markdown","metadata":{"id":"Y4cxMUX5FF7F"},"source":["\n","\n","---\n","\n"]},{"cell_type":"markdown","metadata":{"id":"ig8CJYW2G7NY"},"source":["**Acknowledgement**: For the usage of dataset below\n","https://archive.ics.uci.edu/ml/machine-learning-databases/00320/student.zip\n","\n","**References**: Dua, D. and Graff, C. (2019). UCI Machine Learning Repository [[http://archive.ics.uci.edu/ml](http://archive.ics.uci.edu/ml)]. Irvine, CA: University of California, School of Information and Computer Science.\n","\n","P. Cortez and A. Silva. Using Data Mining to Predict Secondary School Student Performance. In A. Brito and J. Teixeira Eds., Proceedings of 5th FUture BUsiness TEChnology Conference (FUBUTEC 2008) pp.5-12, Porto, Portugal, April, 2008, EUROSIS, ISBN 978-9077381-39-7.\n","[http://www3.dsi.uminho.pt/pcortez/student.pdf](http://www3.dsi.uminho.pt/pcortez/student.pdf)"]}],"metadata":{"anaconda-cloud":{},"colab":{"provenance":[{"file_id":"1gM7svL2n3J7iMAWvPqw3brnjeqhQfHw-","timestamp":1625840295433}],"toc_visible":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":0}