{ "cells": [ { "cell_type": "markdown", "id": "38aececf", "metadata": {}, "source": [ "# pandas: Sorting" ] }, { "cell_type": "code", "execution_count": 1, "id": "9da58f4c", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.524418Z", "iopub.status.busy": "2025-09-02T18:58:31.524297Z", "iopub.status.idle": "2025-09-02T18:58:31.710052Z", "shell.execute_reply": "2025-09-02T18:58:31.709416Z" }, "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "id": "65dd3025", "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": "45408077", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.711760Z", "iopub.status.busy": "2025-09-02T18:58:31.711595Z", "iopub.status.idle": "2025-09-02T18:58:31.716259Z", "shell.execute_reply": "2025-09-02T18:58:31.715657Z" }, "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "data = pd.read_csv('data/Pokemon.csv')" ] }, { "cell_type": "code", "execution_count": 3, "id": "40455394", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.717563Z", "iopub.status.busy": "2025-09-02T18:58:31.717443Z", "iopub.status.idle": "2025-09-02T18:58:31.725544Z", "shell.execute_reply": "2025-09-02T18:58:31.725138Z" }, "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": "96476903", "metadata": {}, "source": [ "## Sort by 1 column, ascending" ] }, { "cell_type": "code", "execution_count": 4, "id": "2f2d5a00", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.726893Z", "iopub.status.busy": "2025-09-02T18:58:31.726779Z", "iopub.status.idle": "2025-09-02T18:58:31.732282Z", "shell.execute_reply": "2025-09-02T18:58:31.731860Z" }, "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
600540SewaddleBugGrass3104553704060425False
136127PinsirBugNaN500651251005570851False
457412BurmyBugNaN2244029452945364False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", "600 540 Sewaddle Bug Grass 310 45 53 70 40 \n", "136 127 Pinsir Bug NaN 500 65 125 100 55 \n", "457 412 Burmy Bug NaN 224 40 29 45 29 \n", "\n", " Sp. Def Speed Generation Legendary \n", "600 60 42 5 False \n", "136 70 85 1 False \n", "457 45 36 4 False " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values('Type 1').head(3)" ] }, { "cell_type": "markdown", "id": "acefbe2f", "metadata": {}, "source": [ "## Sort by 1 column, descending" ] }, { "cell_type": "code", "execution_count": 5, "id": "ab2f9895", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.733480Z", "iopub.status.busy": "2025-09-02T18:58:31.733371Z", "iopub.status.idle": "2025-09-02T18:58:31.738722Z", "shell.execute_reply": "2025-09-02T18:58:31.738252Z" }, "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
371339BarboachWaterGround2885048434641603False
9790ShellderWaterNaN30530651004525401False
240222CorsolaWaterRock3805555856585352False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", "371 339 Barboach Water Ground 288 50 48 43 46 \n", "97 90 Shellder Water NaN 305 30 65 100 45 \n", "240 222 Corsola Water Rock 380 55 55 85 65 \n", "\n", " Sp. Def Speed Generation Legendary \n", "371 41 60 3 False \n", "97 25 40 1 False \n", "240 85 35 2 False " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values('Type 1', ascending=False).head(3)" ] }, { "cell_type": "markdown", "id": "cbf486ef", "metadata": {}, "source": [ "## Sort by multiple columns, 1 = ascending, 0 = descending" ] }, { "cell_type": "code", "execution_count": 6, "id": "9b9441c9", "metadata": { "execution": { "iopub.execute_input": "2025-09-02T18:58:31.740032Z", "iopub.status.busy": "2025-09-02T18:58:31.739925Z", "iopub.status.idle": "2025-09-02T18:58:31.750043Z", "shell.execute_reply": "2025-09-02T18:58:31.749664Z" }, "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
520469YanmegaBugFlying51586768611656954False
698637VolcaronaBugFire5508560651351051005False
231214HeracrossBugFighting50080125754095852False
\n", "
" ], "text/plain": [ " # Name Type 1 Type 2 Total HP Attack Defense Sp. Atk \\\n", "520 469 Yanmega Bug Flying 515 86 76 86 116 \n", "698 637 Volcarona Bug Fire 550 85 60 65 135 \n", "231 214 Heracross Bug Fighting 500 80 125 75 40 \n", "\n", " Sp. Def Speed Generation Legendary \n", "520 56 95 4 False \n", "698 105 100 5 False \n", "231 95 85 2 False " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort_values(['Type 1', 'HP'], ascending=[1, 0]).head(3)" ] } ], "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 }