pandas: Sorting#
import pandas as pd
Loading data into Pandas DataFrame#
The dataset is from Kaggle - Pokemon.
data = pd.read_csv('data/Pokemon.csv')
data
Show code cell output
Hide code cell output
# | Name | Type 1 | Type 2 | Total | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | Bulbasaur | Grass | Poison | 318 | 45 | 49 | 49 | 65 | 65 | 45 | 1 | False |
1 | 2 | Ivysaur | Grass | Poison | 405 | 60 | 62 | 63 | 80 | 80 | 60 | 1 | False |
2 | 3 | Venusaur | Grass | Poison | 525 | 80 | 82 | 83 | 100 | 100 | 80 | 1 | False |
3 | 3 | VenusaurMega Venusaur | Grass | Poison | 625 | 80 | 100 | 123 | 122 | 120 | 80 | 1 | False |
4 | 4 | Charmander | Fire | NaN | 309 | 39 | 52 | 43 | 60 | 50 | 65 | 1 | False |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
795 | 719 | Diancie | Rock | Fairy | 600 | 50 | 100 | 150 | 100 | 150 | 50 | 6 | True |
796 | 719 | DiancieMega Diancie | Rock | Fairy | 700 | 50 | 160 | 110 | 160 | 110 | 110 | 6 | True |
797 | 720 | HoopaHoopa Confined | Psychic | Ghost | 600 | 80 | 110 | 60 | 150 | 130 | 70 | 6 | True |
798 | 720 | HoopaHoopa Unbound | Psychic | Dark | 680 | 80 | 160 | 60 | 170 | 130 | 80 | 6 | True |
799 | 721 | Volcanion | Fire | Water | 600 | 80 | 110 | 120 | 130 | 90 | 70 | 6 | True |
800 rows × 13 columns
Sort by 1 column, ascending#
data.sort_values('Type 1').head(3)
Show code cell output
Hide code cell output
# | Name | Type 1 | Type 2 | Total | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
600 | 540 | Sewaddle | Bug | Grass | 310 | 45 | 53 | 70 | 40 | 60 | 42 | 5 | False |
136 | 127 | Pinsir | Bug | NaN | 500 | 65 | 125 | 100 | 55 | 70 | 85 | 1 | False |
457 | 412 | Burmy | Bug | NaN | 224 | 40 | 29 | 45 | 29 | 45 | 36 | 4 | False |
Sort by 1 column, descending#
data.sort_values('Type 1', ascending=False).head(3)
Show code cell output
Hide code cell output
# | Name | Type 1 | Type 2 | Total | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
371 | 339 | Barboach | Water | Ground | 288 | 50 | 48 | 43 | 46 | 41 | 60 | 3 | False |
97 | 90 | Shellder | Water | NaN | 305 | 30 | 65 | 100 | 45 | 25 | 40 | 1 | False |
240 | 222 | Corsola | Water | Rock | 380 | 55 | 55 | 85 | 65 | 85 | 35 | 2 | False |
Sort by multiple columns, 1 = ascending, 0 = descending#
data.sort_values(['Type 1', 'HP'], ascending=[1, 0]).head(3)
Show code cell output
Hide code cell output
# | Name | Type 1 | Type 2 | Total | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
520 | 469 | Yanmega | Bug | Flying | 515 | 86 | 76 | 86 | 116 | 56 | 95 | 4 | False |
698 | 637 | Volcarona | Bug | Fire | 550 | 85 | 60 | 65 | 135 | 105 | 100 | 5 | False |
231 | 214 | Heracross | Bug | Fighting | 500 | 80 | 125 | 75 | 40 | 95 | 85 | 2 | False |