{ "cells": [ { "cell_type": "markdown", "id": "bd0bdfe1", "metadata": {}, "source": [ "# pandas: `iloc` and `loc` in Pandas" ] }, { "cell_type": "code", "execution_count": 1, "id": "3506cc2c", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.009229Z", "iopub.status.busy": "2025-09-02T18:59:22.009072Z", "iopub.status.idle": "2025-09-02T18:59:22.237564Z", "shell.execute_reply": "2025-09-02T18:59:22.236937Z" }, "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "id": "558440ee", "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": 2, "id": "be0dd82a", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.239405Z", "iopub.status.busy": "2025-09-02T18:59:22.239253Z", "iopub.status.idle": "2025-09-02T18:59:22.243797Z", "shell.execute_reply": "2025-09-02T18:59:22.243272Z" }, "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "data = pd.read_csv('data/Pokemon.csv')" ] }, { "cell_type": "code", "execution_count": 3, "id": "437b42cb", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.245111Z", "iopub.status.busy": "2025-09-02T18:59:22.245002Z", "iopub.status.idle": "2025-09-02T18:59:22.253660Z", "shell.execute_reply": "2025-09-02T18:59:22.253035Z" }, "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", " \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
..........................................
795719DiancieRockFairy60050100150100150506True
796719DiancieMega DiancieRockFairy700501601101601101106True
797720HoopaHoopa ConfinedPsychicGhost6008011060150130706True
798720HoopaHoopa UnboundPsychicDark6808016060170130806True
799721VolcanionFireWater6008011012013090706True
\n", "

800 rows × 13 columns

\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", "795 719 Diancie Rock Fairy 600 50 100 150 \n", "796 719 DiancieMega Diancie Rock Fairy 700 50 160 110 \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", "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 \n", ".. ... ... ... ... ... \n", "795 100 150 50 6 True \n", "796 160 110 110 6 True \n", "797 150 130 70 6 True \n", "798 170 130 80 6 True \n", "799 130 90 70 6 True \n", "\n", "[800 rows x 13 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "id": "0241c756", "metadata": {}, "source": [ "## `iloc`" ] }, { "cell_type": "code", "execution_count": 4, "id": "059815ca", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.254932Z", "iopub.status.busy": "2025-09-02T18:59:22.254826Z", "iopub.status.idle": "2025-09-02T18:59:22.258124Z", "shell.execute_reply": "2025-09-02T18:59:22.257592Z" }, "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "# 3\n", "Name VenusaurMega Venusaur\n", "Type 1 Grass\n", "Type 2 Poison\n", "Total 625\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", "Name: 3, dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read single row\n", "data.iloc[3]" ] }, { "cell_type": "code", "execution_count": 5, "id": "24a519c5", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.259274Z", "iopub.status.busy": "2025-09-02T18:59:22.259171Z", "iopub.status.idle": "2025-09-02T18:59:22.263856Z", "shell.execute_reply": "2025-09-02T18:59:22.263480Z" }, "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 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
33VenusaurMega VenusaurGrassPoison62580100123122120801False
66CharizardFireFlying534788478109851001False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "6 6 Charizard Fire Flying 534 78 84 78 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "3 122 120 80 1 False \n", "6 109 85 100 1 False " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read multiple rows\n", "data.iloc[[3,6]]" ] }, { "cell_type": "code", "execution_count": 6, "id": "395184ec", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.264965Z", "iopub.status.busy": "2025-09-02T18:59:22.264854Z", "iopub.status.idle": "2025-09-02T18:59:22.269501Z", "shell.execute_reply": "2025-09-02T18:59:22.269114Z" }, "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
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
55CharmeleonFireNaN4055864588065801False
66CharizardFireFlying534788478109851001False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense \\\n", "3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 \n", "4 4 Charmander Fire NaN 309 39 52 43 \n", "5 5 Charmeleon Fire NaN 405 58 64 58 \n", "6 6 Charizard Fire Flying 534 78 84 78 \n", "\n", " Sp. Atk Sp. Def Speed Generation Legendary \n", "3 122 120 80 1 False \n", "4 60 50 65 1 False \n", "5 80 65 80 1 False \n", "6 109 85 100 1 False " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read a range of rows\n", "data.iloc[3:7]" ] }, { "cell_type": "markdown", "id": "3d8e983f", "metadata": {}, "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": 7, "id": "d95f1508", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.270752Z", "iopub.status.busy": "2025-09-02T18:59:22.270642Z", "iopub.status.idle": "2025-09-02T18:59:22.276769Z", "shell.execute_reply": "2025-09-02T18:59:22.276412Z" }, "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
156144ArticunoIceFlying580908510095125851True
157145ZapdosElectricFlying580909085125901001True
158146MoltresFireFlying580901009012585901True
162150MewtwoPsychicNaN68010611090154901301True
163150MewtwoMega Mewtwo XPsychicFighting7801061901001541001301True
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack \\\n", "156 144 Articuno Ice Flying 580 90 85 \n", "157 145 Zapdos Electric Flying 580 90 90 \n", "158 146 Moltres Fire Flying 580 90 100 \n", "162 150 Mewtwo Psychic NaN 680 106 110 \n", "163 150 MewtwoMega Mewtwo X Psychic Fighting 780 106 190 \n", "\n", " Defense Sp. Atk Sp. Def Speed Generation Legendary \n", "156 100 95 125 85 1 True \n", "157 85 125 90 100 1 True \n", "158 90 125 85 90 1 True \n", "162 90 154 90 130 1 True \n", "163 100 154 100 130 1 True " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Legendary'] == True].head()" ] }, { "cell_type": "markdown", "id": "a482cbf8", "metadata": {}, "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": 8, "id": "3f76a5e5", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:59:22.278158Z", "iopub.status.busy": "2025-09-02T18:59:22.278033Z", "iopub.status.idle": "2025-09-02T18:59:22.282390Z", "shell.execute_reply": "2025-09-02T18:59:22.282005Z" }, "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": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data['Attack'] == 49, ['Name']]" ] } ], "metadata": { "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 }