{ "cells": [ { "cell_type": "markdown", "id": "a81f2b55", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "# pandas: Pandas Basics Exploration\n" ] }, { "cell_type": "markdown", "id": "a3f2d2b1", "metadata": {}, "source": [ "## Loading data into Pandas DataFrame\n", "The dataset is from [Kaggle - Pokemon](https://www.kaggle.com/datasets/abcsds/pokemon)." ] }, { "cell_type": "code", "execution_count": 1, "id": "ed3d1345", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "169d342c", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "data = pd.read_csv('data/Pokemon.csv')" ] }, { "cell_type": "code", "execution_count": 3, "id": "4cf5d845", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "((800, 13), 2)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape, data.ndim" ] }, { "cell_type": "markdown", "id": "66a430ea", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Get headers" ] }, { "cell_type": "code", "execution_count": 4, "id": "a43f487f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Index(['#', 'Name', 'Type 1', 'Type 2', 'Total', 'HP', 'Attack', 'Defense',\n", " 'Sp. Atk', 'Sp. Def', 'Speed', 'Generation', 'Legendary'],\n", " dtype='object')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.columns" ] }, { "cell_type": "markdown", "id": "02ed73a0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## First and last rows" ] }, { "cell_type": "code", "execution_count": 5, "id": "9c840236", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "( # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def \\\n", " 0 1 Bulbasaur Grass Poison 318 45 49 49 65 65 \n", " \n", " Speed Generation Legendary \n", " 0 45 1 False ,\n", " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", " 799 721 Volcanion Fire Water 600 80 110 120 130 \n", " \n", " Sp. Def Speed Generation Legendary \n", " 799 90 70 6 True )" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head(1), data.tail(1)" ] }, { "cell_type": "markdown", "id": "6b425f02", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## High level description of the data" ] }, { "cell_type": "code", "execution_count": 6, "id": "771074ce", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#TotalHPAttackDefenseSp. AtkSp. DefSpeedGeneration
count800.000000800.00000800.000000800.000000800.000000800.000000800.000000800.000000800.00000
mean362.813750435.1025069.25875079.00125073.84250072.82000071.90250068.2775003.32375
std208.343798119.9630425.53466932.45736631.18350132.72229427.82891629.0604741.66129
min1.000000180.000001.0000005.0000005.00000010.00000020.0000005.0000001.00000
25%184.750000330.0000050.00000055.00000050.00000049.75000050.00000045.0000002.00000
50%364.500000450.0000065.00000075.00000070.00000065.00000070.00000065.0000003.00000
75%539.250000515.0000080.000000100.00000090.00000095.00000090.00000090.0000005.00000
max721.000000780.00000255.000000190.000000230.000000194.000000230.000000180.0000006.00000
\n", "
" ], "text/plain": [ " # Total HP Attack Defense Sp. Atk \\\n", "count 800.000000 800.00000 800.000000 800.000000 800.000000 800.000000 \n", "mean 362.813750 435.10250 69.258750 79.001250 73.842500 72.820000 \n", "std 208.343798 119.96304 25.534669 32.457366 31.183501 32.722294 \n", "min 1.000000 180.00000 1.000000 5.000000 5.000000 10.000000 \n", "25% 184.750000 330.00000 50.000000 55.000000 50.000000 49.750000 \n", "50% 364.500000 450.00000 65.000000 75.000000 70.000000 65.000000 \n", "75% 539.250000 515.00000 80.000000 100.000000 90.000000 95.000000 \n", "max 721.000000 780.00000 255.000000 190.000000 230.000000 194.000000 \n", "\n", " Sp. Def Speed Generation \n", "count 800.000000 800.000000 800.00000 \n", "mean 71.902500 68.277500 3.32375 \n", "std 27.828916 29.060474 1.66129 \n", "min 20.000000 5.000000 1.00000 \n", "25% 50.000000 45.000000 2.00000 \n", "50% 70.000000 65.000000 3.00000 \n", "75% 90.000000 90.000000 5.00000 \n", "max 230.000000 180.000000 6.00000 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.describe()" ] }, { "cell_type": "markdown", "id": "788401e5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Get specific columns" ] }, { "cell_type": "code", "execution_count": 7, "id": "cdc123ea", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Name HP Legendary\n", "0 Bulbasaur 45 False\n", "1 Ivysaur 60 False\n", "2 Venusaur 80 False\n", "3 VenusaurMega Venusaur 80 False\n", "4 Charmander 39 False\n" ] } ], "source": [ "multiple_columns = data[['Name', 'HP', 'Legendary']]\n", "print(multiple_columns.head())" ] }, { "cell_type": "markdown", "id": "571c1301", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Get specific rows" ] }, { "cell_type": "code", "execution_count": 8, "id": "a303b066", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# 1\n", "Name Bulbasaur\n", "Type 1 Grass\n", "Type 2 Poison\n", "Total 318\n", "HP 45\n", "Attack 49\n", "Defense 49\n", "Sp. Atk 65\n", "Sp. Def 65\n", "Speed 45\n", "Generation 1\n", "Legendary False\n", "Name: 0, dtype: object\n" ] } ], "source": [ "# Get the first row, which contains the feature names\n", "middle_row = data.iloc[0]\n", "print(middle_row)" ] }, { "cell_type": "markdown", "id": "ad05195e", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Get specific coordinates item in the DataFrame" ] }, { "cell_type": "code", "execution_count": 9, "id": "839b2de6", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Bulbasaur\n", "1 Ivysaur\n", "2 Venusaur\n", "Name: Name, dtype: object\n" ] } ], "source": [ "# Get the name of the first 3 rows\n", "item = data.iloc[[0, 1, 2], 1]\n", "print(item)" ] }, { "cell_type": "markdown", "id": "fb4da884", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Remove a column" ] }, { "cell_type": "markdown", "id": "d65c3d5d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "- Here, if you do not use the `columns` parameter, you need to specify the axis (0 for rows and 1 for columns)." ] }, { "cell_type": "code", "execution_count": 10, "id": "737cf502", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison4549496565451False
12IvysaurGrassPoison6062638080601False
23VenusaurGrassPoison808283100100801False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 \n", "1 2 Ivysaur Grass Poison 60 62 63 80 80 60 \n", "2 3 Venusaur Grass Poison 80 82 83 100 100 80 \n", "\n", " Generation Legendary \n", "0 1 False \n", "1 1 False \n", "2 1 False " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here, 'Total' column is the sum of HP,Attack,Defense, SP. Atk, SP. Def, Speed.\n", "dropped_data = data.drop('Total', axis=1)\n", "dropped_data.head(3)" ] }, { "cell_type": "markdown", "id": "6e299495", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "- Alternatively, you can use the `columns` parameter to specify the column(s) to drop without needing to specify the axis.\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "2f7eef98", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison4549496565451False
12IvysaurGrassPoison6062638080601False
23VenusaurGrassPoison808283100100801False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 \n", "1 2 Ivysaur Grass Poison 60 62 63 80 80 60 \n", "2 3 Venusaur Grass Poison 80 82 83 100 100 80 \n", "\n", " Generation Legendary \n", "0 1 False \n", "1 1 False \n", "2 1 False " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dropped_data = data.drop(columns='Total')\n", "dropped_data.head(3)" ] }, { "cell_type": "markdown", "id": "cd50d00d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "- `inplace=True` modifies the original DataFrame without needing to reassign it." ] }, { "cell_type": "code", "execution_count": 12, "id": "7ec40eb0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison4549496565451False
12IvysaurGrassPoison6062638080601False
23VenusaurGrassPoison808283100100801False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 \n", "1 2 Ivysaur Grass Poison 60 62 63 80 80 60 \n", "2 3 Venusaur Grass Poison 80 82 83 100 100 80 \n", "\n", " Generation Legendary \n", "0 1 False \n", "1 1 False \n", "2 1 False " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.drop(columns='Total', inplace=True)\n", "data.head(3)" ] }, { "cell_type": "markdown", "id": "b0f9151f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Add a column" ] }, { "cell_type": "code", "execution_count": 13, "id": "a6a1eab4", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
01BulbasaurGrassPoison4549496565451False318
12IvysaurGrassPoison6062638080601False405
23VenusaurGrassPoison808283100100801False525
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 \n", "1 2 Ivysaur Grass Poison 60 62 63 80 80 60 \n", "2 3 Venusaur Grass Poison 80 82 83 100 100 80 \n", "\n", " Generation Legendary Total \n", "0 1 False 318 \n", "1 1 False 405 \n", "2 1 False 525 " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# So, in order to add the 'Total' column again, we do the summation\n", "data['Total'] = data.iloc[:, 4:10].sum(axis=1)\n", "data.head(3)" ] }, { "cell_type": "markdown", "id": "50482fd1", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## `iloc` and `loc` in Pandas" ] }, { "cell_type": "markdown", "id": "9ed5f42e", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### `iloc`" ] }, { "cell_type": "code", "execution_count": 14, "id": "7d651573", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "# 3\n", "Name VenusaurMega Venusaur\n", "Type 1 Grass\n", "Type 2 Poison\n", "HP 80\n", "Attack 100\n", "Defense 123\n", "Sp. Atk 122\n", "Sp. Def 120\n", "Speed 80\n", "Generation 1\n", "Legendary False\n", "Total 625\n", "Name: 3, dtype: object" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read single row\n", "data.iloc[3]" ] }, { "cell_type": "code", "execution_count": 15, "id": "2152c557", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
33VenusaurMega VenusaurGrassPoison80100123122120801False625
66CharizardFireFlying788478109851001False534
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk \\\n", "3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 \n", "6 6 Charizard Fire Flying 78 84 78 109 \n", "\n", " Sp. Def Speed Generation Legendary Total \n", "3 120 80 1 False 625 \n", "6 85 100 1 False 534 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read multiple rows\n", "data.iloc[[3,6]]" ] }, { "cell_type": "code", "execution_count": 16, "id": "4f47d5ca", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
33VenusaurMega VenusaurGrassPoison80100123122120801False625
44CharmanderFireNaN3952436050651False309
55CharmeleonFireNaN5864588065801False405
66CharizardFireFlying788478109851001False534
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk \\\n", "3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 \n", "4 4 Charmander Fire NaN 39 52 43 60 \n", "5 5 Charmeleon Fire NaN 58 64 58 80 \n", "6 6 Charizard Fire Flying 78 84 78 109 \n", "\n", " Sp. Def Speed Generation Legendary Total \n", "3 120 80 1 False 625 \n", "4 50 65 1 False 309 \n", "5 65 80 1 False 405 \n", "6 85 100 1 False 534 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read a range of rows\n", "data.iloc[3:7]" ] }, { "cell_type": "markdown", "id": "1fc21eb2", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### `loc`\n", "\n", "The `loc` property in Pandas is used to access a group of rows and columns by labels or a boolean array. Unlike `iloc`, which uses integer-based indexing, `loc` uses the actual labels of the index and columns. This makes it very useful for selecting data based on meaningful row or column names." ] }, { "cell_type": "code", "execution_count": 17, "id": "6940beb1", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
156144ArticunoIceFlying908510095125851True580
157145ZapdosElectricFlying909085125901001True580
158146MoltresFireFlying901009012585901True580
162150MewtwoPsychicNaN10611090154901301True680
163150MewtwoMega Mewtwo XPsychicFighting1061901001541001301True780
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense \\\n", "156 144 Articuno Ice Flying 90 85 100 \n", "157 145 Zapdos Electric Flying 90 90 85 \n", "158 146 Moltres Fire Flying 90 100 90 \n", "162 150 Mewtwo Psychic NaN 106 110 90 \n", "163 150 MewtwoMega Mewtwo X Psychic Fighting 106 190 100 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary Total \n", "156 95 125 85 1 True 580 \n", "157 125 90 100 1 True 580 \n", "158 125 85 90 1 True 580 \n", "162 154 90 130 1 True 680 \n", "163 154 100 130 1 True 780 " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Legendary'] == True].head()" ] }, { "cell_type": "markdown", "id": "e41c4c0e", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "If you have a condition to filter rows, then you must pass the columns that you want to select after the comma as a sequence. e.g., `[condition, ['col1', 'col2']]`" ] }, { "cell_type": "code", "execution_count": 18, "id": "a2827bfd", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
Name
0Bulbasaur
166Chikorita
506Finneon
\n", "
" ], "text/plain": [ " Name\n", "0 Bulbasaur\n", "166 Chikorita\n", "506 Finneon" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Attack'] == 49, ['Name']]" ] }, { "cell_type": "markdown", "id": "271211e3", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Filtering data" ] }, { "cell_type": "markdown", "id": "53c4b775", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "We can filter with multiple conditions at ease." ] }, { "cell_type": "code", "execution_count": 19, "id": "6bceaa1c", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
01BulbasaurGrassPoison4549496565451False318
12IvysaurGrassPoison6062638080601False405
23VenusaurGrassPoison808283100100801False525
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 \n", "1 2 Ivysaur Grass Poison 60 62 63 80 80 60 \n", "2 3 Venusaur Grass Poison 80 82 83 100 100 80 \n", "\n", " Generation Legendary Total \n", "0 1 False 318 \n", "1 1 False 405 \n", "2 1 False 525 " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[(data['Type 1'] == 'Grass') & (data['Type 2'] == 'Poison')].head(3)" ] }, { "cell_type": "code", "execution_count": 20, "id": "fd09e643", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
5045VileplumeGrassPoison75808511090501False490
10194GengarGhostPoison606560130751101False500
197182BellossomGrassNaN75809590100502False490
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "50 45 Vileplume Grass Poison 75 80 85 110 90 \n", "101 94 Gengar Ghost Poison 60 65 60 130 75 \n", "197 182 Bellossom Grass NaN 75 80 95 90 100 \n", "\n", " Speed Generation Legendary Total \n", "50 50 1 False 490 \n", "101 110 1 False 500 \n", "197 50 2 False 490 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[(data['Type 1'] == 'Grass') | (data['Type 2'] == 'Poison')][0::10].loc[data['HP'] > 50].head(3)" ] }, { "cell_type": "markdown", "id": "5001ab59", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Regex Filtering" ] }, { "cell_type": "code", "execution_count": 21, "id": "de6c65c4", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import re" ] }, { "cell_type": "markdown", "id": "0a9e23c7", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Explicit filtering" ] }, { "cell_type": "code", "execution_count": 22, "id": "e4b72a56", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
33VenusaurMega VenusaurGrassPoison80100123122120801False625
76CharizardMega Charizard XFireDragon78130111130851001False634
86CharizardMega Charizard YFireFlying78104781591151001False634
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk \\\n", "3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 \n", "7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 \n", "8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 \n", "\n", " Sp. Def Speed Generation Legendary Total \n", "3 120 80 1 False 625 \n", "7 85 100 1 False 634 \n", "8 115 100 1 False 634 " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Name'].str.contains('Mega')].head(3)" ] }, { "cell_type": "markdown", "id": "0af7793d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Regex filtering, case sensitive by default" ] }, { "cell_type": "code", "execution_count": 23, "id": "f78fe3ea", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
44CharmanderFireNaN3952436050651False309
55CharmeleonFireNaN5864588065801False405
66CharizardFireFlying788478109851001False534
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed \\\n", "4 4 Charmander Fire NaN 39 52 43 60 50 65 \n", "5 5 Charmeleon Fire NaN 58 64 58 80 65 80 \n", "6 6 Charizard Fire Flying 78 84 78 109 85 100 \n", "\n", " Generation Legendary Total \n", "4 1 False 309 \n", "5 1 False 405 \n", "6 1 False 534 " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Type 1'].str.contains('Fire|grass', regex=True)].head(3)" ] }, { "cell_type": "markdown", "id": "12a704bb", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Regex filtering, case insensitive" ] }, { "cell_type": "code", "execution_count": 24, "id": "f2d7f808", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
01BulbasaurGrassPoison4549496565451False318
12IvysaurGrassPoison6062638080601False405
23VenusaurGrassPoison808283100100801False525
33VenusaurMega VenusaurGrassPoison80100123122120801False625
44CharmanderFireNaN3952436050651False309
55CharmeleonFireNaN5864588065801False405
66CharizardFireFlying788478109851001False534
76CharizardMega Charizard XFireDragon78130111130851001False634
86CharizardMega Charizard YFireFlying78104781591151001False634
4237VulpixFireNaN3841405065651False299
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 45 49 49 \n", "1 2 Ivysaur Grass Poison 60 62 63 \n", "2 3 Venusaur Grass Poison 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 80 100 123 \n", "4 4 Charmander Fire NaN 39 52 43 \n", "5 5 Charmeleon Fire NaN 58 64 58 \n", "6 6 Charizard Fire Flying 78 84 78 \n", "7 6 CharizardMega Charizard X Fire Dragon 78 130 111 \n", "8 6 CharizardMega Charizard Y Fire Flying 78 104 78 \n", "42 37 Vulpix Fire NaN 38 41 40 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary Total \n", "0 65 65 45 1 False 318 \n", "1 80 80 60 1 False 405 \n", "2 100 100 80 1 False 525 \n", "3 122 120 80 1 False 625 \n", "4 60 50 65 1 False 309 \n", "5 80 65 80 1 False 405 \n", "6 109 85 100 1 False 534 \n", "7 130 85 100 1 False 634 \n", "8 159 115 100 1 False 634 \n", "42 50 65 65 1 False 299 " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Type 1'].str.contains('Fire|grass', flags=re.IGNORECASE, regex=True)].head(10)" ] }, { "cell_type": "markdown", "id": "8759f438", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Using regex" ] }, { "cell_type": "code", "execution_count": 25, "id": "4895963f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
1310CaterpieBugNaN4530352020451False195
2016PidgeyNormalFlying4045403535561False251
2117PidgeottoNormalFlying6360555050711False349
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "13 10 Caterpie Bug NaN 45 30 35 20 20 \n", "20 16 Pidgey Normal Flying 40 45 40 35 35 \n", "21 17 Pidgeotto Normal Flying 63 60 55 50 50 \n", "\n", " Speed Generation Legendary Total \n", "13 45 1 False 195 \n", "20 56 1 False 251 \n", "21 71 1 False 349 " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Name'].str.contains('pi[a-z]*', flags=re.IGNORECASE, regex=True)].head(3)" ] }, { "cell_type": "code", "execution_count": 26, "id": "4cbbb98d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
2016PidgeyNormalFlying4045403535561False251
2117PidgeottoNormalFlying6360555050711False349
2218PidgeotNormalFlying83807570701011False479
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "20 16 Pidgey Normal Flying 40 45 40 35 35 \n", "21 17 Pidgeotto Normal Flying 63 60 55 50 50 \n", "22 18 Pidgeot Normal Flying 83 80 75 70 70 \n", "\n", " Speed Generation Legendary Total \n", "20 56 1 False 251 \n", "21 71 1 False 349 \n", "22 101 1 False 479 " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Name'].str.contains('^pi[a-z]*', flags=re.IGNORECASE, regex=True)].head(3)" ] }, { "cell_type": "code", "execution_count": 27, "id": "0621e04a", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
1310CaterpieBugNaN4530352020451False195
2016PidgeyNormalFlying4045403535561False251
2117PidgeottoNormalFlying6360555050711False349
2218PidgeotNormalFlying83807570701011False479
2318PidgeotMega PidgeotNormalFlying838080135801211False579
3025PikachuElectricNaN3555405050901False320
4237VulpixFireNaN3841405065651False299
7670WeepinbellGrassPoison6590508545551False390
8478RapidashFireNaN651007080801051False500
136127PinsirBugNaN651251005570851False500
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk \\\n", "13 10 Caterpie Bug NaN 45 30 35 20 \n", "20 16 Pidgey Normal Flying 40 45 40 35 \n", "21 17 Pidgeotto Normal Flying 63 60 55 50 \n", "22 18 Pidgeot Normal Flying 83 80 75 70 \n", "23 18 PidgeotMega Pidgeot Normal Flying 83 80 80 135 \n", "30 25 Pikachu Electric NaN 35 55 40 50 \n", "42 37 Vulpix Fire NaN 38 41 40 50 \n", "76 70 Weepinbell Grass Poison 65 90 50 85 \n", "84 78 Rapidash Fire NaN 65 100 70 80 \n", "136 127 Pinsir Bug NaN 65 125 100 55 \n", "\n", " Sp. Def Speed Generation Legendary Total \n", "13 20 45 1 False 195 \n", "20 35 56 1 False 251 \n", "21 50 71 1 False 349 \n", "22 70 101 1 False 479 \n", "23 80 121 1 False 579 \n", "30 50 90 1 False 320 \n", "42 65 65 1 False 299 \n", "76 45 55 1 False 390 \n", "84 80 105 1 False 500 \n", "136 70 85 1 False 500 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Name'].str.contains('pi.*', flags=re.IGNORECASE, regex=True)].head(10)" ] }, { "cell_type": "markdown", "id": "a498fac2", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Sorting" ] }, { "cell_type": "markdown", "id": "b2b369b9", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Sort by 1 column, ascending" ] }, { "cell_type": "code", "execution_count": 28, "id": "55b0c767", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
600540SewaddleBugGrass4553704060425False310
136127PinsirBugNaN651251005570851False500
457412BurmyBugNaN4029452945364False224
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "600 540 Sewaddle Bug Grass 45 53 70 40 60 \n", "136 127 Pinsir Bug NaN 65 125 100 55 70 \n", "457 412 Burmy Bug NaN 40 29 45 29 45 \n", "\n", " Speed Generation Legendary Total \n", "600 42 5 False 310 \n", "136 85 1 False 500 \n", "457 36 4 False 224 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values('Type 1').head(3)" ] }, { "cell_type": "markdown", "id": "e2418588", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Sort by 1 column, descending" ] }, { "cell_type": "code", "execution_count": 29, "id": "015f6314", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
371339BarboachWaterGround5048434641603False288
9790ShellderWaterNaN30651004525401False305
240222CorsolaWaterRock5555856585352False380
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "371 339 Barboach Water Ground 50 48 43 46 41 \n", "97 90 Shellder Water NaN 30 65 100 45 25 \n", "240 222 Corsola Water Rock 55 55 85 65 85 \n", "\n", " Speed Generation Legendary Total \n", "371 60 3 False 288 \n", "97 40 1 False 305 \n", "240 35 2 False 380 " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values('Type 1', ascending=False).head(3)" ] }, { "cell_type": "markdown", "id": "2c92d590", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Sort by multiple columns, 1 = ascending, 0 = descending" ] }, { "cell_type": "code", "execution_count": 30, "id": "7103be68", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
520469YanmegaBugFlying86768611656954False515
698637VolcaronaBugFire8560651351051005False550
231214HeracrossBugFighting80125754095852False500
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "520 469 Yanmega Bug Flying 86 76 86 116 56 \n", "698 637 Volcarona Bug Fire 85 60 65 135 105 \n", "231 214 Heracross Bug Fighting 80 125 75 40 95 \n", "\n", " Speed Generation Legendary Total \n", "520 95 4 False 515 \n", "698 100 5 False 550 \n", "231 85 2 False 500 " ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values(['Type 1', 'HP'], ascending=[1, 0]).head(3)" ] }, { "cell_type": "markdown", "id": "5a51ca29", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Reset index" ] }, { "cell_type": "markdown", "id": "0f012ce5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "In the cell below, you can see the index is not consecutive (the leftmost column). We can reset it with the `reset_index()` method. By default, it adds the old index as a new column. If you do not want that, use the `drop=True` parameter." ] }, { "cell_type": "code", "execution_count": 31, "id": "da438222", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
5045VileplumeGrassPoison75808511090501False490
10194GengarGhostPoison606560130751101False500
197182BellossomGrassNaN75809590100502False490
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "50 45 Vileplume Grass Poison 75 80 85 110 90 \n", "101 94 Gengar Ghost Poison 60 65 60 130 75 \n", "197 182 Bellossom Grass NaN 75 80 95 90 100 \n", "\n", " Speed Generation Legendary Total \n", "50 50 1 False 490 \n", "101 110 1 False 500 \n", "197 50 2 False 490 " ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_data = data.loc[(data['Type 1'] == 'Grass') | (data['Type 2'] == 'Poison')][0::10].loc[data['HP'] > 50].head(3)\n", "new_data" ] }, { "cell_type": "code", "execution_count": 32, "id": "76ccd83f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2HPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendaryTotal
045VileplumeGrassPoison75808511090501False490
194GengarGhostPoison606560130751101False500
2182BellossomGrassNaN75809590100502False490
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def \\\n", "0 45 Vileplume Grass Poison 75 80 85 110 90 \n", "1 94 Gengar Ghost Poison 60 65 60 130 75 \n", "2 182 Bellossom Grass NaN 75 80 95 90 100 \n", "\n", " Speed Generation Legendary Total \n", "0 50 1 False 490 \n", "1 110 1 False 500 \n", "2 50 2 False 490 " ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_data.reset_index(drop=True, inplace=True)\n", "new_data" ] }, { "cell_type": "markdown", "id": "3fe96623", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Group by/Aggregate using aggregate function" ] }, { "cell_type": "markdown", "id": "e33fb4d0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "Aggregate functions like `mean()`, `sum()`, `min()`, and `max()` in pandas only work with numerical data. Non-numeric columns (such as strings or booleans) cannot be averaged or summed, so you must select only numeric columns when using these functions to avoid errors and get meaningful results." ] }, { "cell_type": "markdown", "id": "1787a494", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Group by 1 column" ] }, { "cell_type": "code", "execution_count": 33, "id": "47c811a5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#HPAttackDefenseSp. AtkSp. DefSpeedGenerationTotal
Type 1
Bug334.49275456.88405870.97101470.72463853.86956564.79710161.6811593.217391378.927536
Dark461.35483966.80645288.38709770.22580674.64516169.51612976.1612904.032258445.741935
Dragon474.37500083.312500112.12500086.37500096.84375088.84375083.0312503.875000550.531250
Electric363.50000059.79545569.09090966.29545590.02272773.70454584.5000003.272727443.409091
Fairy449.52941274.11764761.52941265.70588278.52941284.70588248.5882354.117647413.176471
Fighting363.85185269.85185296.77777865.92592653.11111164.70370466.0740743.370370416.444444
Fire327.40384669.90384684.76923167.76923188.98076972.21153874.4423083.211538458.076923
Flying677.75000070.75000078.75000066.25000094.25000072.500000102.5000005.500000485.000000
Ghost486.50000064.43750073.78125081.18750079.34375076.46875064.3437504.187500439.562500
Grass344.87142967.27142973.21428670.80000077.50000070.42857161.9285713.357143421.142857
\n", "
" ], "text/plain": [ " # HP Attack Defense Sp. Atk Sp. Def \\\n", "Type 1 \n", "Bug 334.492754 56.884058 70.971014 70.724638 53.869565 64.797101 \n", "Dark 461.354839 66.806452 88.387097 70.225806 74.645161 69.516129 \n", "Dragon 474.375000 83.312500 112.125000 86.375000 96.843750 88.843750 \n", "Electric 363.500000 59.795455 69.090909 66.295455 90.022727 73.704545 \n", "Fairy 449.529412 74.117647 61.529412 65.705882 78.529412 84.705882 \n", "Fighting 363.851852 69.851852 96.777778 65.925926 53.111111 64.703704 \n", "Fire 327.403846 69.903846 84.769231 67.769231 88.980769 72.211538 \n", "Flying 677.750000 70.750000 78.750000 66.250000 94.250000 72.500000 \n", "Ghost 486.500000 64.437500 73.781250 81.187500 79.343750 76.468750 \n", "Grass 344.871429 67.271429 73.214286 70.800000 77.500000 70.428571 \n", "\n", " Speed Generation Total \n", "Type 1 \n", "Bug 61.681159 3.217391 378.927536 \n", "Dark 76.161290 4.032258 445.741935 \n", "Dragon 83.031250 3.875000 550.531250 \n", "Electric 84.500000 3.272727 443.409091 \n", "Fairy 48.588235 4.117647 413.176471 \n", "Fighting 66.074074 3.370370 416.444444 \n", "Fire 74.442308 3.211538 458.076923 \n", "Flying 102.500000 5.500000 485.000000 \n", "Ghost 64.343750 4.187500 439.562500 \n", "Grass 61.928571 3.357143 421.142857 " ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Select only numeric columns for aggregation\n", "numeric_cols = data.select_dtypes(include='number').columns\n", "data.groupby(['Type 1'])[numeric_cols].mean().sort_values('Type 1').head(10)" ] }, { "cell_type": "markdown", "id": "ea7a4f03", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Group by hierarchical columns" ] }, { "cell_type": "code", "execution_count": 34, "id": "5e12978b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#HPAttackDefenseSp. AtkSp. DefSpeedGenerationTotal
Type 1Type 2
BugElectric222222222
Fighting222222222
Fire222222222
Flying141414141414141414
Ghost111111111
\n", "
" ], "text/plain": [ " # HP Attack Defense Sp. Atk Sp. Def Speed Generation \\\n", "Type 1 Type 2 \n", "Bug Electric 2 2 2 2 2 2 2 2 \n", " Fighting 2 2 2 2 2 2 2 2 \n", " Fire 2 2 2 2 2 2 2 2 \n", " Flying 14 14 14 14 14 14 14 14 \n", " Ghost 1 1 1 1 1 1 1 1 \n", "\n", " Total \n", "Type 1 Type 2 \n", "Bug Electric 2 \n", " Fighting 2 \n", " Fire 2 \n", " Flying 14 \n", " Ghost 1 " ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.groupby(['Type 1', 'Type 2'])[numeric_cols].count().head()" ] }, { "cell_type": "markdown", "id": "0c9fa804", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Quizzes" ] }, { "cell_type": "markdown", "id": "7e4b6f73", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 1. Import the pandas library as pd." ] }, { "cell_type": "code", "execution_count": 35, "id": "fc1f336f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "id": "fb62a2ec", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 2. Read CSV into df" ] }, { "cell_type": "code", "execution_count": 36, "id": "4ec813c7", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "df = pd.read_csv('data/Pokemon.csv')" ] }, { "cell_type": "markdown", "id": "584296db", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 3. Show first 5 rows" ] }, { "cell_type": "code", "execution_count": 37, "id": "c212416f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "4 4 Charmander Fire NaN 309 39 52 43 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "1 80 80 60 1 False \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "4 60 50 65 1 False " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(5)" ] }, { "cell_type": "markdown", "id": "fd504b6e", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 4. Show last 3 rows" ] }, { "cell_type": "code", "execution_count": 38, "id": "be05a6d0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
797720HoopaHoopa ConfinedPsychicGhost6008011060150130706True
798720HoopaHoopa UnboundPsychicDark6808016060170130806True
799721VolcanionFireWater6008011012013090706True
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "797 720 HoopaHoopa Confined Psychic Ghost 600 80 110 60 \n", "798 720 HoopaHoopa Unbound Psychic Dark 680 80 160 60 \n", "799 721 Volcanion Fire Water 600 80 110 120 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "797 150 130 70 6 True \n", "798 170 130 80 6 True \n", "799 130 90 70 6 True " ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.tail(3)" ] }, { "cell_type": "markdown", "id": "449f7910", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 5. Show all column names" ] }, { "cell_type": "code", "execution_count": 39, "id": "b2caf235", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Index(['#', 'Name', 'Type 1', 'Type 2', 'Total', 'HP', 'Attack', 'Defense',\n", " 'Sp. Atk', 'Sp. Def', 'Speed', 'Generation', 'Legendary'],\n", " dtype='object')" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "markdown", "id": "e57ac681", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 6. Select rows index 10 to 15" ] }, { "cell_type": "code", "execution_count": 40, "id": "b0fee3ab", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
108WartortleWaterNaN4055963806580581False
119BlastoiseWaterNaN530798310085105781False
129BlastoiseMega BlastoiseWaterNaN63079103120135115781False
1310CaterpieBugNaN1954530352020451False
1411MetapodBugNaN2055020552525301False
1512ButterfreeBugFlying3956045509080701False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "10 8 Wartortle Water NaN 405 59 63 80 \n", "11 9 Blastoise Water NaN 530 79 83 100 \n", "12 9 BlastoiseMega Blastoise Water NaN 630 79 103 120 \n", "13 10 Caterpie Bug NaN 195 45 30 35 \n", "14 11 Metapod Bug NaN 205 50 20 55 \n", "15 12 Butterfree Bug Flying 395 60 45 50 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "10 65 80 58 1 False \n", "11 85 105 78 1 False \n", "12 135 115 78 1 False \n", "13 20 20 45 1 False \n", "14 25 25 30 1 False \n", "15 90 80 70 1 False " ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[10:16]" ] }, { "cell_type": "markdown", "id": "61eba367", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 7. Select row index 50" ] }, { "cell_type": "code", "execution_count": 41, "id": "1c380040", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "# 45\n", "Name Vileplume\n", "Type 1 Grass\n", "Type 2 Poison\n", "Total 490\n", "HP 75\n", "Attack 80\n", "Defense 85\n", "Sp. Atk 110\n", "Sp. Def 90\n", "Speed 50\n", "Generation 1\n", "Legendary False\n", "Name: 50, dtype: object" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[50]" ] }, { "cell_type": "markdown", "id": "f9a74604", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 8. Filter rows Type 1 == Grass" ] }, { "cell_type": "code", "execution_count": 42, "id": "561871b6", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
4843OddishGrassPoison3204550557565301False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "48 43 Oddish Grass Poison 320 45 50 55 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "1 80 80 60 1 False \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "48 75 65 30 1 False " ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['Type 1'] == 'Grass'].head()" ] }, { "cell_type": "markdown", "id": "db03cdc0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 9. Filter rows HP > 100" ] }, { "cell_type": "code", "execution_count": 43, "id": "99a294ff", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
4439JigglypuffNormalFairy27011545204525201False
4540WigglytuffNormalFairy43514070458550451False
9689MukPoisonNaN5001051057565100501False
120112RhydonGroundRock4851051301204545401False
121113ChanseyNormalNaN4502505535105501False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", "44 39 Jigglypuff Normal Fairy 270 115 45 20 45 \n", "45 40 Wigglytuff Normal Fairy 435 140 70 45 85 \n", "96 89 Muk Poison NaN 500 105 105 75 65 \n", "120 112 Rhydon Ground Rock 485 105 130 120 45 \n", "121 113 Chansey Normal NaN 450 250 5 5 35 \n", "\n", " Sp. Def Speed Generation Legendary \n", "44 25 20 1 False \n", "45 50 45 1 False \n", "96 100 50 1 False \n", "120 45 40 1 False \n", "121 105 50 1 False " ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['HP'] > 100].head()" ] }, { "cell_type": "markdown", "id": "f8fc7387", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 10. Filter rows Name contains chu" ] }, { "cell_type": "code", "execution_count": 44, "id": "4db3c928", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
3025PikachuElectricNaN3203555405050901False
3126RaichuElectricNaN48560905590801101False
186172PichuElectricNaN2052040153535602False
257238SmoochumIcePsychic3054530158565652False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", "30 25 Pikachu Electric NaN 320 35 55 40 50 \n", "31 26 Raichu Electric NaN 485 60 90 55 90 \n", "186 172 Pichu Electric NaN 205 20 40 15 35 \n", "257 238 Smoochum Ice Psychic 305 45 30 15 85 \n", "\n", " Sp. Def Speed Generation Legendary \n", "30 50 90 1 False \n", "31 80 110 1 False \n", "186 35 60 2 False \n", "257 65 65 2 False " ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['Name'].str.contains('chu')].head()" ] }, { "cell_type": "markdown", "id": "8b80dd7a", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 11. Sort by Attack descending" ] }, { "cell_type": "code", "execution_count": 45, "id": "805a3fd8", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
163150MewtwoMega Mewtwo XPsychicFighting7801061901001541001301True
232214HeracrossMega HeracrossBugFighting6008018511540105752False
429386DeoxysAttack FormePsychicNaN6005018020180201503True
426384RayquazaMega RayquazaDragonFlying7801051801001801001153True
424383GroudonPrimal GroudonGroundFire77010018016015090903True
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack \\\n", "163 150 MewtwoMega Mewtwo X Psychic Fighting 780 106 190 \n", "232 214 HeracrossMega Heracross Bug Fighting 600 80 185 \n", "429 386 DeoxysAttack Forme Psychic NaN 600 50 180 \n", "426 384 RayquazaMega Rayquaza Dragon Flying 780 105 180 \n", "424 383 GroudonPrimal Groudon Ground Fire 770 100 180 \n", "\n", " Defense Sp. Atk Sp. Def Speed Generation Legendary \n", "163 100 154 100 130 1 True \n", "232 115 40 105 75 2 False \n", "429 20 180 20 150 3 True \n", "426 100 180 100 115 3 True \n", "424 160 150 90 90 3 True " ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sort_values('Attack', ascending=False).head()" ] }, { "cell_type": "markdown", "id": "4e7c0200", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 12. Reset index" ] }, { "cell_type": "code", "execution_count": 46, "id": "ffa56c30", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "4 4 Charmander Fire NaN 309 39 52 43 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "1 80 80 60 1 False \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "4 60 50 65 1 False " ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_df = df.reset_index(drop=True)\n", "new_df.head()" ] }, { "cell_type": "markdown", "id": "3ac5c009", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 13. Select columns Name, Type 1, HP" ] }, { "cell_type": "code", "execution_count": 47, "id": "0e7ff79e", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
NameType 1HP
0BulbasaurGrass45
1IvysaurGrass60
2VenusaurGrass80
3VenusaurMega VenusaurGrass80
4CharmanderFire39
\n", "
" ], "text/plain": [ " Name Type 1 HP\n", "0 Bulbasaur Grass 45\n", "1 Ivysaur Grass 60\n", "2 Venusaur Grass 80\n", "3 VenusaurMega Venusaur Grass 80\n", "4 Charmander Fire 39" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[['Name', 'Type 1', 'HP']].head()" ] }, { "cell_type": "markdown", "id": "37d4cca9", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 14. Filter Type 1 == Grass and Type 2 == Poison" ] }, { "cell_type": "code", "execution_count": 48, "id": "185eb2d4", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
4843OddishGrassPoison3204550557565301False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "48 43 Oddish Grass Poison 320 45 50 55 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "1 80 80 60 1 False \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "48 75 65 30 1 False " ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['Type 1'] == 'Grass') & (df['Type 2'] == 'Poison')].head()" ] }, { "cell_type": "markdown", "id": "6ea10292", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 15. Mean HP of Legendary" ] }, { "cell_type": "code", "execution_count": 49, "id": "15ead6c8", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "np.float64(92.73846153846154)" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['Legendary'] == True]['HP'].mean()" ] }, { "cell_type": "markdown", "id": "07643484", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 16. Rows with even indices" ] }, { "cell_type": "code", "execution_count": 50, "id": "b204c987", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
23VenusaurGrassPoison525808283100100801False
44CharmanderFireNaN3093952436050651False
66CharizardFireFlying534788478109851001False
86CharizardMega Charizard YFireFlying63478104781591151001False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "4 4 Charmander Fire NaN 309 39 52 43 \n", "6 6 Charizard Fire Flying 534 78 84 78 \n", "8 6 CharizardMega Charizard Y Fire Flying 634 78 104 78 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "2 100 100 80 1 False \n", "4 60 50 65 1 False \n", "6 109 85 100 1 False \n", "8 159 115 100 1 False " ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "even_indices_df = df.iloc[::2]\n", "even_indices_df.head()" ] }, { "cell_type": "markdown", "id": "fdb4e23b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 17. Save even indices to filtered_pokemon.csv" ] }, { "cell_type": "code", "execution_count": 51, "id": "365ac05c", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
23VenusaurGrassPoison525808283100100801False
44CharmanderFireNaN3093952436050651False
66CharizardFireFlying534788478109851001False
86CharizardMega Charizard YFireFlying63478104781591151001False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "4 4 Charmander Fire NaN 309 39 52 43 \n", "6 6 Charizard Fire Flying 534 78 84 78 \n", "8 6 CharizardMega Charizard Y Fire Flying 634 78 104 78 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "2 100 100 80 1 False \n", "4 60 50 65 1 False \n", "6 109 85 100 1 False \n", "8 159 115 100 1 False " ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "even_indices_df = df.iloc[::2]\n", "even_indices_df.to_csv('data/filtered_pokemon.csv', index=True)\n", "even_indices_df = pd.read_csv('data/filtered_pokemon.csv', index_col=0)\n", "even_indices_df.head()" ] }, { "cell_type": "markdown", "id": "a8eb8c37", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 18. First 10 rows Name, Type 1, Attack" ] }, { "cell_type": "code", "execution_count": 52, "id": "bac8faca", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
NameType 1Attack
0BulbasaurGrass49
1IvysaurGrass62
2VenusaurGrass82
3VenusaurMega VenusaurGrass100
4CharmanderFire52
5CharmeleonFire64
6CharizardFire84
7CharizardMega Charizard XFire130
8CharizardMega Charizard YFire104
9SquirtleWater48
\n", "
" ], "text/plain": [ " Name Type 1 Attack\n", "0 Bulbasaur Grass 49\n", "1 Ivysaur Grass 62\n", "2 Venusaur Grass 82\n", "3 VenusaurMega Venusaur Grass 100\n", "4 Charmander Fire 52\n", "5 Charmeleon Fire 64\n", "6 Charizard Fire 84\n", "7 CharizardMega Charizard X Fire 130\n", "8 CharizardMega Charizard Y Fire 104\n", "9 Squirtle Water 48" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[['Name', 'Type 1', 'Attack']].head(10)" ] }, { "cell_type": "markdown", "id": "f6030e44", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 19. Max Defense" ] }, { "cell_type": "code", "execution_count": 53, "id": "76bc40a3", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "np.int64(230)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['Defense'].max()" ] }, { "cell_type": "markdown", "id": "00676f32", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 20. Rows Speed above mean" ] }, { "cell_type": "code", "execution_count": 54, "id": "babc2ff5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean speed: 68.2775\n" ] }, { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
55CharmeleonFireNaN4055864588065801False
66CharizardFireFlying534788478109851001False
76CharizardMega Charizard XFireDragon63478130111130851001False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "5 5 Charmeleon Fire NaN 405 58 64 58 \n", "6 6 Charizard Fire Flying 534 78 84 78 \n", "7 6 CharizardMega Charizard X Fire Dragon 634 78 130 111 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "5 80 65 80 1 False \n", "6 109 85 100 1 False \n", "7 130 85 100 1 False " ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_speed = df['Speed'].mean()\n", "new_df = df.loc[df['Speed'] > mean_speed]\n", "print('mean speed: ', mean_speed)\n", "new_df.head()" ] }, { "cell_type": "markdown", "id": "a49e5f22", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 21. First 3 rows Name and Attack" ] }, { "cell_type": "code", "execution_count": 55, "id": "5b8db16b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
NameAttack
0Bulbasaur49
1Ivysaur62
2Venusaur82
\n", "
" ], "text/plain": [ " Name Attack\n", "0 Bulbasaur 49\n", "1 Ivysaur 62\n", "2 Venusaur 82" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[['Name', 'Attack']].iloc[:3]" ] }, { "cell_type": "markdown", "id": "d7249a78", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 22. Count per Type 1" ] }, { "cell_type": "code", "execution_count": 56, "id": "5aef00dc", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Type 1\n", "Bug 69\n", "Dark 31\n", "Dragon 32\n", "Electric 44\n", "Fairy 17\n", "Fighting 27\n", "Fire 52\n", "Flying 4\n", "Ghost 32\n", "Grass 70\n", "Ground 32\n", "Ice 24\n", "Normal 98\n", "Poison 28\n", "Psychic 57\n", "Rock 44\n", "Steel 27\n", "Water 112\n", "Name: #, dtype: int64" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby(['Type 1'])['#'].count().sort_index()" ] }, { "cell_type": "markdown", "id": "e88432f1", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 23. Rows Generation 3 or 5" ] }, { "cell_type": "code", "execution_count": 57, "id": "50421198", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
272252TreeckoGrassNaN3104045356555703False
273253GrovyleGrassNaN4055065458565953False
274254SceptileGrassNaN530708565105851203False
275254SceptileMega SceptileGrassDragon6307011075145851453False
276255TorchicFireNaN3104560407050453False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "272 252 Treecko Grass NaN 310 40 45 35 \n", "273 253 Grovyle Grass NaN 405 50 65 45 \n", "274 254 Sceptile Grass NaN 530 70 85 65 \n", "275 254 SceptileMega Sceptile Grass Dragon 630 70 110 75 \n", "276 255 Torchic Fire NaN 310 45 60 40 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "272 65 55 70 3 False \n", "273 85 65 95 3 False \n", "274 105 85 120 3 False \n", "275 145 85 145 3 False \n", "276 70 50 45 3 False " ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['Generation'] == 3) | (df['Generation'] == 5)].head()" ] }, { "cell_type": "markdown", "id": "8e951a15", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 24. Replace Fire with Flame in Type 1" ] }, { "cell_type": "code", "execution_count": 58, "id": "d191d51b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFlameNaN3093952436050651False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "0 1 Bulbasaur Grass Poison 318 45 49 49 \n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "2 3 Venusaur Grass Poison 525 80 82 83 \n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "4 4 Charmander Flame NaN 309 39 52 43 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "0 65 65 45 1 False \n", "1 80 80 60 1 False \n", "2 100 100 80 1 False \n", "3 122 120 80 1 False \n", "4 60 50 65 1 False " ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_copy = df.copy()\n", "df_copy['Type 1'] = df_copy['Type 1'].str.replace('Fire', 'Flame')\n", "df_copy.head()" ] }, { "cell_type": "markdown", "id": "bea4f4d9", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 25. Rows Name startswith P" ] }, { "cell_type": "code", "execution_count": 59, "id": "e723a12b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
2016PidgeyNormalFlying2514045403535561False
2117PidgeottoNormalFlying3496360555050711False
2218PidgeotNormalFlying47983807570701011False
2318PidgeotMega PidgeotNormalFlying579838080135801211False
3025PikachuElectricNaN3203555405050901False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "20 16 Pidgey Normal Flying 251 40 45 40 \n", "21 17 Pidgeotto Normal Flying 349 63 60 55 \n", "22 18 Pidgeot Normal Flying 479 83 80 75 \n", "23 18 PidgeotMega Pidgeot Normal Flying 579 83 80 80 \n", "30 25 Pikachu Electric NaN 320 35 55 40 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "20 35 35 56 1 False \n", "21 50 50 71 1 False \n", "22 70 70 101 1 False \n", "23 135 80 121 1 False \n", "30 50 50 90 1 False " ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['Name'].str.startswith('P')].head()" ] }, { "cell_type": "markdown", "id": "ab450b41", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 26. Mean Attack per Type 1" ] }, { "cell_type": "code", "execution_count": 60, "id": "de03d400", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Type 1\n", "Bug 70.971014\n", "Dark 88.387097\n", "Dragon 112.125000\n", "Electric 69.090909\n", "Fairy 61.529412\n", "Fighting 96.777778\n", "Fire 84.769231\n", "Flying 78.750000\n", "Ghost 73.781250\n", "Grass 73.214286\n", "Ground 95.750000\n", "Ice 72.750000\n", "Normal 73.469388\n", "Poison 74.678571\n", "Psychic 71.456140\n", "Rock 92.863636\n", "Steel 92.703704\n", "Water 74.151786\n", "Name: Attack, dtype: float64" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('Type 1')['Attack'].mean().sort_index()" ] }, { "cell_type": "markdown", "id": "278b4a40", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 27. Sort by Type 1 asc, Attack desc" ] }, { "cell_type": "code", "execution_count": 61, "id": "ace242fb", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
232214HeracrossMega HeracrossBugFighting6008018511540105752False
137127PinsirMega PinsirBugFlying6006515512065901051False
1915BeedrillMega BeedrillBugPoison495651504015801451False
229212ScizorMega ScizorBugSteel6007015014065100752False
650589EscavalierBugSteel4957013510560105205False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack \\\n", "232 214 HeracrossMega Heracross Bug Fighting 600 80 185 \n", "137 127 PinsirMega Pinsir Bug Flying 600 65 155 \n", "19 15 BeedrillMega Beedrill Bug Poison 495 65 150 \n", "229 212 ScizorMega Scizor Bug Steel 600 70 150 \n", "650 589 Escavalier Bug Steel 495 70 135 \n", "\n", " Defense Sp. Atk Sp. Def Speed Generation Legendary \n", "232 115 40 105 75 2 False \n", "137 120 65 90 105 1 False \n", "19 40 15 80 145 1 False \n", "229 140 65 100 75 2 False \n", "650 105 60 105 20 5 False " ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sort_values(['Type 1', 'Attack'], ascending=[1, 0]).head()" ] }, { "cell_type": "markdown", "id": "a35220b1", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 28. Mean Defense per Type 1" ] }, { "cell_type": "code", "execution_count": 62, "id": "6915d0d8", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Type 1\n", "Bug 70.724638\n", "Dark 70.225806\n", "Dragon 86.375000\n", "Electric 66.295455\n", "Fairy 65.705882\n", "Fighting 65.925926\n", "Fire 67.769231\n", "Flying 66.250000\n", "Ghost 81.187500\n", "Grass 70.800000\n", "Ground 84.843750\n", "Ice 71.416667\n", "Normal 59.846939\n", "Poison 68.821429\n", "Psychic 67.684211\n", "Rock 100.795455\n", "Steel 126.370370\n", "Water 72.946429\n", "Name: Defense, dtype: float64" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('Type 1')['Defense'].mean().sort_index()" ] }, { "cell_type": "markdown", "id": "0faa7d05", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 29. Legendary with Attack > 100" ] }, { "cell_type": "code", "execution_count": 63, "id": "8ffd23a0", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
162150MewtwoPsychicNaN68010611090154901301True
163150MewtwoMega Mewtwo XPsychicFighting7801061901001541001301True
164150MewtwoMega Mewtwo YPsychicNaN780106150701941201401True
263244EnteiFireNaN5801151158590751002True
270250Ho-ohFireFlying68010613090110154902True
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "162 150 Mewtwo Psychic NaN 680 106 110 90 \n", "163 150 MewtwoMega Mewtwo X Psychic Fighting 780 106 190 100 \n", "164 150 MewtwoMega Mewtwo Y Psychic NaN 780 106 150 70 \n", "263 244 Entei Fire NaN 580 115 115 85 \n", "270 250 Ho-oh Fire Flying 680 106 130 90 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "162 154 90 130 1 True \n", "163 154 100 130 1 True \n", "164 194 120 140 1 True \n", "263 90 75 100 2 True \n", "270 110 154 90 2 True " ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['Legendary'] == True) & (df['Attack'] > 100)].head()" ] }, { "cell_type": "markdown", "id": "09aabdc7", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 30. New DataFrame Name and Total renamed Overall" ] }, { "cell_type": "code", "execution_count": 64, "id": "219e9729", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
NameOverall
0Bulbasaur318
1Ivysaur405
2Venusaur525
3VenusaurMega Venusaur625
4Charmander309
\n", "
" ], "text/plain": [ " Name Overall\n", "0 Bulbasaur 318\n", "1 Ivysaur 405\n", "2 Venusaur 525\n", "3 VenusaurMega Venusaur 625\n", "4 Charmander 309" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_df = df[['Name', 'Total']].rename(columns={'Total': 'Overall'})\n", "new_df.head()" ] }, { "cell_type": "markdown", "id": "a61bb6e6", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 31. Filter Type 1 == Water and Attack >= 80" ] }, { "cell_type": "code", "execution_count": 65, "id": "ad6c72f5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
119BlastoiseWaterNaN530798310085105781False
129BlastoiseMega BlastoiseWaterNaN63079103120135115781False
6055GolduckWaterNaN5008082789580851False
6762PoliwrathWaterFighting5109095957090701False
9891CloysterWaterIce52550951808545701False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "11 9 Blastoise Water NaN 530 79 83 100 \n", "12 9 BlastoiseMega Blastoise Water NaN 630 79 103 120 \n", "60 55 Golduck Water NaN 500 80 82 78 \n", "67 62 Poliwrath Water Fighting 510 90 95 95 \n", "98 91 Cloyster Water Ice 525 50 95 180 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "11 85 105 78 1 False \n", "12 135 115 78 1 False \n", "60 95 80 85 1 False \n", "67 70 90 70 1 False \n", "98 85 45 70 1 False " ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['Type 1'] == 'Water') & (df['Attack'] >= 80)].head()" ] }, { "cell_type": "markdown", "id": "ae2a07cb", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 32. Generation 4 Name and Defense" ] }, { "cell_type": "code", "execution_count": 66, "id": "001f624f", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
NameDefense
432Turtwig64
433Grotle85
434Torterra105
435Chimchar44
436Monferno52
\n", "
" ], "text/plain": [ " Name Defense\n", "432 Turtwig 64\n", "433 Grotle 85\n", "434 Torterra 105\n", "435 Chimchar 44\n", "436 Monferno 52" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[df['Generation'] == 4][['Name', 'Defense']].head()" ] }, { "cell_type": "markdown", "id": "ebf3f377", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 33. Mean Total per (Type 1, Type 2)" ] }, { "cell_type": "code", "execution_count": 67, "id": "e40463fd", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "Type 1 Type 2 \n", "Bug Electric 395.500000\n", " Fighting 550.000000\n", " Fire 455.000000\n", " Flying 419.500000\n", " Ghost 236.000000\n", " Grass 384.000000\n", " Ground 345.000000\n", " Poison 347.916667\n", " Rock 435.000000\n", " Steel 509.714286\n", "Name: Total, dtype: float64" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby(['Type 1', 'Type 2'])['Total'].mean().head(10)" ] }, { "cell_type": "markdown", "id": "a39e168a", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### 34. Filter Total between 400 and 500" ] }, { "cell_type": "code", "execution_count": 68, "id": "392b4d89", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "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", "
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
12IvysaurGrassPoison4056062638080601False
55CharmeleonFireNaN4055864588065801False
108WartortleWaterNaN4055963806580581False
1915BeedrillMega BeedrillBugPoison495651504015801451False
2218PidgeotNormalFlying47983807570701011False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "1 2 Ivysaur Grass Poison 405 60 62 63 \n", "5 5 Charmeleon Fire NaN 405 58 64 58 \n", "10 8 Wartortle Water NaN 405 59 63 80 \n", "19 15 BeedrillMega Beedrill Bug Poison 495 65 150 40 \n", "22 18 Pidgeot Normal Flying 479 83 80 75 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "1 80 80 60 1 False \n", "5 80 65 80 1 False \n", "10 65 80 58 1 False \n", "19 15 80 145 1 False \n", "22 70 70 101 1 False " ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.loc[(df['Total'] >= 400) & (df['Total'] <= 500)].head()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (code-venv)", "language": "python", "name": "code-venv" }, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }