0% found this document useful (0 votes)
12 views17 pages

Lec 16 Pandas - Continue

The document describes a data analysis process using a Pokémon dataset, which includes various attributes such as HP, Attack, Defense, and types. It demonstrates operations like calculating total stats, filtering legendary Pokémon, and modifying type names to lower or upper case. The analysis also involves grouping Pokémon by their legendary status and generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views17 pages

Lec 16 Pandas - Continue

The document describes a data analysis process using a Pokémon dataset, which includes various attributes such as HP, Attack, Defense, and types. It demonstrates operations like calculating total stats, filtering legendary Pokémon, and modifying type names to lower or upper case. The analysis also involves grouping Pokémon by their legendary status and generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

import pandas as pd

import numpy as np

df=pd.read_csv('D:\\AEGIS\\Python\\pokemon.csv')

df

# Name Type 1 Type 2 HP Attack Defense Sp.


Atk \
0 1 Bulbasaur Grass Poison 45 49 49
65
1 2 Ivysaur Grass Poison 60 62 63
80
2 3 Venusaur Grass Poison 80 82 83
100
3 4 Mega Venusaur Grass Poison 80 100 123
122
4 5 Charmander Fire NaN 39 52 43
60
.. ... ... ... ... .. ... ... ..
.
795 796 Diancie Rock Fairy 50 100 150
100
796 797 Mega Diancie Rock Fairy 50 160 110
160
797 798 Hoopa Confined Psychic Ghost 80 110 60
150
798 799 Hoopa Unbound Psychic Dark 80 160 60
170
799 800 Volcanion Fire Water 80 110 120
130

Sp. Def Speed Generation Legendary


0 65 45 1 False
1 80 60 1 False
2 100 80 1 False
3 120 80 1 False
4 50 65 1 False
.. ... ... ... ...
795 150 50 6 True
796 110 110 6 True
797 130 70 6 True
798 130 80 6 True
799 90 70 6 True

[800 rows x 12 columns]

df['Total']=df['HP']+df['Attack']+df['Defense']+df['Sp. Atk']+df['Sp.
Def']+df['Speed']

df
# Name Type 1 Type 2 HP Attack Defense Sp.
Atk \
0 1 Bulbasaur Grass Poison 45 49 49
65
1 2 Ivysaur Grass Poison 60 62 63
80
2 3 Venusaur Grass Poison 80 82 83
100
3 4 Mega Venusaur Grass Poison 80 100 123
122
4 5 Charmander Fire NaN 39 52 43
60
.. ... ... ... ... .. ... ... ..
.
795 796 Diancie Rock Fairy 50 100 150
100
796 797 Mega Diancie Rock Fairy 50 160 110
160
797 798 Hoopa Confined Psychic Ghost 80 110 60
150
798 799 Hoopa Unbound Psychic Dark 80 160 60
170
799 800 Volcanion Fire Water 80 110 120
130

Sp. Def Speed Generation Legendary Total


0 65 45 1 False 318
1 80 60 1 False 405
2 100 80 1 False 525
3 120 80 1 False 625
4 50 65 1 False 309
.. ... ... ... ... ...
795 150 50 6 True 600
796 110 110 6 True 700
797 130 70 6 True 600
798 130 80 6 True 680
799 90 70 6 True 600

[800 rows x 13 columns]

df.drop(['#'],inplace=True,axis=1)

df.rename(columns={'HP':'Health Points'},inplace=True)

df=df.set_index('Name')

df

Type 1 Type 2 Health Points Attack Defense Sp.


Atk \
Name
Bulbasaur Grass Poison 45 49 49
65
Ivysaur Grass Poison 60 62 63
80
Venusaur Grass Poison 80 82 83
100
Mega Venusaur Grass Poison 80 100 123
122
Charmander Fire NaN 39 52 43
60
... ... ... ... ... ... .
..
Diancie Rock Fairy 50 100 150
100
Mega Diancie Rock Fairy 50 160 110
160
Hoopa Confined Psychic Ghost 80 110 60
150
Hoopa Unbound Psychic Dark 80 160 60
170
Volcanion Fire Water 80 110 120
130

Sp. Def Speed Generation Legendary Total


Name
Bulbasaur 65 45 1 False 318
Ivysaur 80 60 1 False 405
Venusaur 100 80 1 False 525
Mega Venusaur 120 80 1 False 625
Charmander 50 65 1 False 309
... ... ... ... ... ...
Diancie 150 50 6 True 600
Mega Diancie 110 110 6 True 700
Hoopa Confined 130 70 6 True 600
Hoopa Unbound 130 80 6 True 680
Volcanion 90 70 6 True 600

[800 rows x 11 columns]

Q. Find how many Legendary Pokemons which


have a single type(Type 1 present and Type 2
absent)
true_legendary=df[df['Legendary']==True]
x=true_legendary['Type 2'].isnull().sum()
print('No of Legendary pokemons which have a single type(type 1
present and type 2 absent) are',x)

No of Legendary pokemons which have a single type(type 1 present and


type 2 absent) are 25

z=df[(df['Legendary']==True) & (df['Type 2'].isnull())]


len(z)

25

Q. Convert all Type 1 to lower case


lower_type1=df['Type 1'].str.lower()
lower_type1

Name
Bulbasaur grass
Ivysaur grass
Venusaur grass
Mega Venusaur grass
Charmander fire
...
Diancie rock
Mega Diancie rock
Hoopa Confined psychic
Hoopa Unbound psychic
Volcanion fire
Name: Type 1, Length: 800, dtype: object

upper_type1=df['Type 1'].str.upper()
upper_type1

Name
Bulbasaur GRASS
Ivysaur GRASS
Venusaur GRASS
Mega Venusaur GRASS
Charmander FIRE
...
Diancie ROCK
Mega Diancie ROCK
Hoopa Confined PSYCHIC
Hoopa Unbound PSYCHIC
Volcanion FIRE
Name: Type 1, Length: 800, dtype: object
Apply()
df['Type 1'].apply(lambda x:x.lower())

Name
Bulbasaur grass
Ivysaur grass
Venusaur grass
Mega Venusaur grass
Charmander fire
...
Diancie rock
Mega Diancie rock
Hoopa Confined psychic
Hoopa Unbound psychic
Volcanion fire
Name: Type 1, Length: 800, dtype: object

isinstance('abc',str)

True

isinstance(10,str)

False

isinstance(10,int)

True

Q. If type 2 present change to upper case else


replace by None
y=df['Type 2'].apply(lambda x:x.upper() if isinstance(x,str) else
None)
y

Name
Bulbasaur POISON
Ivysaur POISON
Venusaur POISON
Mega Venusaur POISON
Charmander None
...
Diancie FAIRY
Mega Diancie FAIRY
Hoopa Confined GHOST
Hoopa Unbound DARK
Volcanion WATER
Name: Type 2, Length: 800, dtype: object

y=df['Type 2'].fillna('None').str.upper()
y

Name
Bulbasaur POISON
Ivysaur POISON
Venusaur POISON
Mega Venusaur POISON
Charmander NONE
...
Diancie FAIRY
Mega Diancie FAIRY
Hoopa Confined GHOST
Hoopa Unbound DARK
Volcanion WATER
Name: Type 2, Length: 800, dtype: object

Groupby()
df.head()

Type 1 Type 2 Health Points Attack Defense Sp. Atk


\
Name

Bulbasaur Grass Poison 45 49 49 65

Ivysaur Grass Poison 60 62 63 80

Venusaur Grass Poison 80 82 83 100

Mega Venusaur Grass Poison 80 100 123 122

Charmander Fire NaN 39 52 43 60

Sp. Def Speed Generation Legendary Total


Name
Bulbasaur 65 45 1 False 318
Ivysaur 80 60 1 False 405
Venusaur 100 80 1 False 525
Mega Venusaur 120 80 1 False 625
Charmander 50 65 1 False 309

df.groupby('Legendary').groups #show different groups as key value


pair
{False: ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Mega Venusaur',
'Charmander', 'Charmeleon', 'Charizard', 'Mega Charizard X', 'Mega
Charizard Y', 'Squirtle', 'Wartortle', 'Blastoise', 'Mega Blastoise',
'Caterpie', 'Metapod', 'Butterfree', 'Weedle', 'Kakuna', 'Beedrill',
'Mega Beedrill', 'Pidgey', 'Pidgeotto', 'Pidgeot', 'Mega Pidgeot',
'Rattata', 'Raticate', 'Spearow', 'Fearow', 'Ekans', 'Arbok',
'Pikachu', 'Raichu', 'Sandshrew', 'Sandslash', 'Nidoran♀', 'Nidorina',
'Nidoqueen', 'Nidoran♂', 'Nidorino', 'Nidoking', 'Clefairy',
'Clefable', 'Vulpix', 'Ninetales', 'Jigglypuff', 'Wigglytuff',
'Zubat', 'Golbat', 'Oddish', 'Gloom', 'Vileplume', 'Paras',
'Parasect', 'Venonat', 'Venomoth', 'Diglett', 'Dugtrio', 'Meowth',
'Persian', 'Psyduck', 'Golduck', 'Mankey', nan, 'Growlithe',
'Arcanine', 'Poliwag', 'Poliwhirl', 'Poliwrath', 'Abra', 'Kadabra',
'Alakazam', 'Mega Alakazam', 'Machop', 'Machoke', 'Machamp',
'Bellsprout', 'Weepinbell', 'Victreebel', 'Tentacool', 'Tentacruel',
'Geodude', 'Graveler', 'Golem', 'Ponyta', 'Rapidash', 'Slowpoke',
'Slowbro', 'Mega Slowbro', 'Magnemite', 'Magneton', 'Farfetch'd',
'Doduo', 'Dodrio', 'Seel', 'Dewgong', 'Grimer', 'Muk', 'Shellder',
'Cloyster', 'Gastly', ...], True: ['Articuno', 'Zapdos', 'Moltres',
'Mewtwo', 'Mega Mewtwo X', 'Mega Mewtwo Y', 'Raikou', 'Entei',
'Suicune', 'Lugia', 'Ho-oh', 'Regirock', 'Regice', 'Registeel',
'Latias', 'Mega Latias', 'Latios', 'Mega Latios', 'Kyogre', 'Primal
Kyogre', 'Groudon', 'Primal Groudon', 'Rayquaza', 'Mega Rayquaza',
'Jirachi', 'Deoxys Normal Forme', 'DeoxysAttack Forme', 'Deoxys
Defense Forme', 'Deoxys Speed Forme', 'Uxie', 'Mesprit', 'Azelf',
'Dialga', 'Palkia', 'Heatran', 'Regigigas', 'Giratina Altered Forme',
'Giratina Origin Forme', 'Darkrai', 'Shaymin Land Forme', 'Shaymin Sky
Forme', 'Arceus', 'Victini', 'Cobalion', 'Terrakion', 'Virizion',
'Tornadus Incarnate Forme', 'Tornadus Therian Forme', 'Thundurus
Incarnate Forme', 'Thundurus Therian Forme', 'Reshiram', 'Zekrom',
'Landorus Incarnate Forme', 'Landorus Therian Forme', 'Kyurem',
'Kyurem Black Kyurem', 'Kyurem White Kyurem', 'Xerneas', 'Yveltal',
'Zygarde Half Forme', 'Diancie', 'Mega Diancie', 'Hoopa Confined',
'Hoopa Unbound', 'Volcanion']}

df.groupby('Generation').groups

{1: ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Mega Venusaur',


'Charmander', 'Charmeleon', 'Charizard', 'Mega Charizard X', 'Mega
Charizard Y', 'Squirtle', 'Wartortle', 'Blastoise', 'Mega Blastoise',
'Caterpie', 'Metapod', 'Butterfree', 'Weedle', 'Kakuna', 'Beedrill',
'Mega Beedrill', 'Pidgey', 'Pidgeotto', 'Pidgeot', 'Mega Pidgeot',
'Rattata', 'Raticate', 'Spearow', 'Fearow', 'Ekans', 'Arbok',
'Pikachu', 'Raichu', 'Sandshrew', 'Sandslash', 'Nidoran♀', 'Nidorina',
'Nidoqueen', 'Nidoran♂', 'Nidorino', 'Nidoking', 'Clefairy',
'Clefable', 'Vulpix', 'Ninetales', 'Jigglypuff', 'Wigglytuff',
'Zubat', 'Golbat', 'Oddish', 'Gloom', 'Vileplume', 'Paras',
'Parasect', 'Venonat', 'Venomoth', 'Diglett', 'Dugtrio', 'Meowth',
'Persian', 'Psyduck', 'Golduck', 'Mankey', nan, 'Growlithe',
'Arcanine', 'Poliwag', 'Poliwhirl', 'Poliwrath', 'Abra', 'Kadabra',
'Alakazam', 'Mega Alakazam', 'Machop', 'Machoke', 'Machamp',
'Bellsprout', 'Weepinbell', 'Victreebel', 'Tentacool', 'Tentacruel',
'Geodude', 'Graveler', 'Golem', 'Ponyta', 'Rapidash', 'Slowpoke',
'Slowbro', 'Mega Slowbro', 'Magnemite', 'Magneton', 'Farfetch'd',
'Doduo', 'Dodrio', 'Seel', 'Dewgong', 'Grimer', 'Muk', 'Shellder',
'Cloyster', 'Gastly', ...], 2: ['Chikorita', 'Bayleef', 'Meganium',
'Cyndaquil', 'Quilava', 'Typhlosion', 'Totodile', 'Croconaw',
'Feraligatr', 'Sentret', 'Furret', 'Hoothoot', 'Noctowl', 'Ledyba',
'Ledian', 'Spinarak', 'Ariados', 'Crobat', 'Chinchou', 'Lanturn',
'Pichu', 'Cleffa', 'Igglybuff', 'Togepi', 'Togetic', 'Natu', 'Xatu',
'Mareep', 'Flaaffy', 'Ampharos', 'Mega Ampharos', 'Bellossom',
'Marill', 'Azumarill', 'Sudowoodo', 'Politoed', 'Hoppip', 'Skiploom',
'Jumpluff', 'Aipom', 'Sunkern', 'Sunflora', 'Yanma', 'Wooper',
'Quagsire', 'Espeon', 'Umbreon', 'Murkrow', 'Slowking', 'Misdreavus',
'Unown', 'Wobbuffet', 'Girafarig', 'Pineco', 'Forretress',
'Dunsparce', 'Gligar', 'Steelix', 'Mega Steelix', 'Snubbull',
'Granbull', 'Qwilfish', 'Scizor', 'Mega Scizor', 'Shuckle',
'Heracross', 'Mega Heracross', 'Sneasel', 'Teddiursa', 'Ursaring',
'Slugma', 'Magcargo', 'Swinub', 'Piloswine', 'Corsola', 'Remoraid',
'Octillery', 'Delibird', 'Mantine', 'Skarmory', 'Houndour',
'Houndoom', 'Mega Houndoom', 'Kingdra', 'Phanpy', 'Donphan',
'Porygon2', 'Stantler', 'Smeargle', 'Tyrogue', 'Hitmontop',
'Smoochum', 'Elekid', 'Magby', 'Miltank', 'Blissey', 'Raikou',
'Entei', 'Suicune', 'Larvitar', ...], 3: ['Treecko', 'Grovyle',
'Sceptile', 'Mega Sceptile', 'Torchic', 'Combusken', 'Blaziken', 'Mega
Blaziken', 'Mudkip', 'Marshtomp', 'Swampert', 'Mega Swampert',
'Poochyena', 'Mightyena', 'Zigzagoon', 'Linoone', 'Wurmple',
'Silcoon', 'Beautifly', 'Cascoon', 'Dustox', 'Lotad', 'Lombre',
'Ludicolo', 'Seedot', 'Nuzleaf', 'Shiftry', 'Taillow', 'Swellow',
'Wingull', 'Pelipper', 'Ralts', 'Kirlia', 'Gardevoir', 'Mega
Gardevoir', 'Surskit', 'Masquerain', 'Shroomish', 'Breloom',
'Slakoth', 'Vigoroth', 'Slaking', 'Nincada', 'Ninjask', 'Shedinja',
'Whismur', 'Loudred', 'Exploud', 'Makuhita', 'Hariyama', 'Azurill',
'Nosepass', 'Skitty', 'Delcatty', 'Sableye', 'Mega Sableye', 'Mawile',
'Mega Mawile', 'Aron', 'Lairon', 'Aggron', 'Mega Aggron', 'Meditite',
'Medicham', 'Mega Medicham', 'Electrike', 'Manectric', 'Mega
Manectric', 'Plusle', 'Minun', 'Volbeat', 'Illumise', 'Roselia',
'Gulpin', 'Swalot', 'Carvanha', 'Sharpedo', 'Mega Sharpedo',
'Wailmer', 'Wailord', 'Numel', 'Camerupt', 'Mega Camerupt', 'Torkoal',
'Spoink', 'Grumpig', 'Spinda', 'Trapinch', 'Vibrava', 'Flygon',
'Cacnea', 'Cacturne', 'Swablu', 'Altaria', 'Mega Altaria', 'Zangoose',
'Seviper', 'Lunatone', 'Solrock', 'Barboach', ...], 4: ['Turtwig',
'Grotle', 'Torterra', 'Chimchar', 'Monferno', 'Infernape', 'Piplup',
'Prinplup', 'Empoleon', 'Starly', 'Staravia', 'Staraptor', 'Bidoof',
'Bibarel', 'Kricketot', 'Kricketune', 'Shinx', 'Luxio', 'Luxray',
'Budew', 'Roserade', 'Cranidos', 'Rampardos', 'Shieldon', 'Bastiodon',
'Burmy', 'Wormadam Plant Cloak', 'Wormadam Sandy Cloak', 'Wormadam
Trash Cloak', 'Mothim', 'Combee', 'Vespiquen', 'Pachirisu', 'Buizel',
'Floatzel', 'Cherubi', 'Cherrim', 'Shellos', 'Gastrodon', 'Ambipom',
'Drifloon', 'Drifblim', 'Buneary', 'Lopunny', 'Mega Lopunny',
'Mismagius', 'Honchkrow', 'Glameow', 'Purugly', 'Chingling', 'Stunky',
'Skuntank', 'Bronzor', 'Bronzong', 'Bonsly', 'Mime Jr.', 'Happiny',
'Chatot', 'Spiritomb', 'Gible', 'Gabite', 'Garchomp', 'Mega Garchomp',
'Munchlax', 'Riolu', 'Lucario', 'Mega Lucario', 'Hippopotas',
'Hippowdon', 'Skorupi', 'Drapion', 'Croagunk', 'Toxicroak',
'Carnivine', 'Finneon', 'Lumineon', 'Mantyke', 'Snover', 'Abomasnow',
'Mega Abomasnow', 'Weavile', 'Magnezone', 'Lickilicky', 'Rhyperior',
'Tangrowth', 'Electivire', 'Magmortar', 'Togekiss', 'Yanmega',
'Leafeon', 'Glaceon', 'Gliscor', 'Mamoswine', 'Porygon-Z', 'Gallade',
'Mega Gallade', 'Probopass', 'Dusknoir', 'Froslass', 'Rotom', ...], 5:
['Victini', 'Snivy', 'Servine', 'Serperior', 'Tepig', 'Pignite',
'Emboar', 'Oshawott', 'Dewott', 'Samurott', 'Patrat', 'Watchog',
'Lillipup', 'Herdier', 'Stoutland', 'Purrloin', 'Liepard', 'Pansage',
'Simisage', 'Pansear', 'Simisear', 'Panpour', 'Simipour', 'Munna',
'Musharna', 'Pidove', 'Tranquill', 'Unfezant', 'Blitzle', 'Zebstrika',
'Roggenrola', 'Boldore', 'Gigalith', 'Woobat', 'Swoobat', 'Drilbur',
'Excadrill', 'Audino', 'Mega Audino', 'Timburr', 'Gurdurr',
'Conkeldurr', 'Tympole', 'Palpitoad', 'Seismitoad', 'Throh', 'Sawk',
'Sewaddle', 'Swadloon', 'Leavanny', 'Venipede', 'Whirlipede',
'Scolipede', 'Cottonee', 'Whimsicott', 'Petilil', 'Lilligant',
'Basculin', 'Sandile', 'Krokorok', 'Krookodile', 'Darumaka',
'Darmanitan Standard Mode', 'Darmanitan Zen Mode', 'Maractus',
'Dwebble', 'Crustle', 'Scraggy', 'Scrafty', 'Sigilyph', 'Yamask',
'Cofagrigus', 'Tirtouga', 'Carracosta', 'Archen', 'Archeops',
'Trubbish', 'Garbodor', 'Zorua', 'Zoroark', 'Minccino', 'Cinccino',
'Gothita', 'Gothorita', 'Gothitelle', 'Solosis', 'Duosion',
'Reuniclus', 'Ducklett', 'Swanna', 'Vanillite', 'Vanillish',
'Vanilluxe', 'Deerling', 'Sawsbuck', 'Emolga', 'Karrablast',
'Escavalier', 'Foongus', 'Amoonguss', ...], 6: ['Chespin',
'Quilladin', 'Chesnaught', 'Fennekin', 'Braixen', 'Delphox',
'Froakie', 'Frogadier', 'Greninja', 'Bunnelby', 'Diggersby',
'Fletchling', 'Fletchinder', 'Talonflame', 'Scatterbug', 'Spewpa',
'Vivillon', 'Litleo', 'Pyroar', 'Flabébé', 'Floette', 'Florges',
'Skiddo', 'Gogoat', 'Pancham', 'Pangoro', 'Furfrou', 'Espurr',
'Meowstic Male', 'Meowstic Female', 'Honedge', 'Doublade', 'Aegislash
Blade Forme', 'Aegislash Shield Forme', 'Spritzee', 'Aromatisse',
'Swirlix', 'Slurpuff', 'Inkay', 'Malamar', 'Binacle', 'Barbaracle',
'Skrelp', 'Dragalge', 'Clauncher', 'Clawitzer', 'Helioptile',
'Heliolisk', 'Tyrunt', 'Tyrantrum', 'Amaura', 'Aurorus', 'Sylveon',
'Hawlucha', 'Dedenne', 'Carbink', 'Goomy', 'Sliggoo', 'Goodra',
'Klefki', 'Phantump', 'Trevenant', 'Pumpkaboo Average Size',
'Pumpkaboo Small Size', 'Pumpkaboo Large Size', 'Pumpkaboo Super
Size', 'Gourgeist Average Size', 'Gourgeist Small Size', 'Gourgeist
Large Size', 'Gourgeist Super Size', 'Bergmite', 'Avalugg', 'Noibat',
'Noivern', 'Xerneas', 'Yveltal', 'Zygarde Half Forme', 'Diancie',
'Mega Diancie', 'Hoopa Confined', 'Hoopa Unbound', 'Volcanion']}

df.groupby(['Generation','Type 1']).groups
{(1, 'Bug'): ['Caterpie', 'Metapod', 'Butterfree', 'Weedle', 'Kakuna',
'Beedrill', 'Mega Beedrill', 'Paras', 'Parasect', 'Venonat',
'Venomoth', 'Scyther', 'Pinsir', 'Mega Pinsir'], (1, 'Dragon'):
['Dratini', 'Dragonair', 'Dragonite'], (1, 'Electric'): ['Pikachu',
'Raichu', 'Magnemite', 'Magneton', 'Voltorb', 'Electrode',
'Electabuzz', 'Jolteon', 'Zapdos'], (1, 'Fairy'): ['Clefairy',
'Clefable'], (1, 'Fighting'): ['Mankey', nan, 'Machop', 'Machoke',
'Machamp', 'Hitmonlee', 'Hitmonchan'], (1, 'Fire'): ['Charmander',
'Charmeleon', 'Charizard', 'Mega Charizard X', 'Mega Charizard Y',
'Vulpix', 'Ninetales', 'Growlithe', 'Arcanine', 'Ponyta', 'Rapidash',
'Magmar', 'Flareon', 'Moltres'], (1, 'Ghost'): ['Gastly', 'Haunter',
'Gengar', 'Mega Gengar'], (1, 'Grass'): ['Bulbasaur', 'Ivysaur',
'Venusaur', 'Mega Venusaur', 'Oddish', 'Gloom', 'Vileplume',
'Bellsprout', 'Weepinbell', 'Victreebel', 'Exeggcute', 'Exeggutor',
'Tangela'], (1, 'Ground'): ['Sandshrew', 'Sandslash', 'Diglett',
'Dugtrio', 'Cubone', 'Marowak', 'Rhyhorn', 'Rhydon'], (1, 'Ice'):
['Jynx', 'Articuno'], (1, 'Normal'): ['Pidgey', 'Pidgeotto',
'Pidgeot', 'Mega Pidgeot', 'Rattata', 'Raticate', 'Spearow', 'Fearow',
'Jigglypuff', 'Wigglytuff', 'Meowth', 'Persian', 'Farfetch'd',
'Doduo', 'Dodrio', 'Lickitung', 'Chansey', 'Kangaskhan', 'Mega
Kangaskhan', 'Tauros', 'Ditto', 'Eevee', 'Porygon', 'Snorlax'], (1,
'Poison'): ['Ekans', 'Arbok', 'Nidoran♀', 'Nidorina', 'Nidoqueen',
'Nidoran♂', 'Nidorino', 'Nidoking', 'Zubat', 'Golbat', 'Grimer',
'Muk', 'Koffing', 'Weezing'], (1, 'Psychic'): ['Abra', 'Kadabra',
'Alakazam', 'Mega Alakazam', 'Drowzee', 'Hypno', 'Mr. Mime', 'Mewtwo',
'Mega Mewtwo X', 'Mega Mewtwo Y', 'Mew'], (1, 'Rock'): ['Geodude',
'Graveler', 'Golem', 'Onix', 'Omanyte', 'Omastar', 'Kabuto',
'Kabutops', 'Aerodactyl', 'Mega Aerodactyl'], (1, 'Water'):
['Squirtle', 'Wartortle', 'Blastoise', 'Mega Blastoise', 'Psyduck',
'Golduck', 'Poliwag', 'Poliwhirl', 'Poliwrath', 'Tentacool',
'Tentacruel', 'Slowpoke', 'Slowbro', 'Mega Slowbro', 'Seel',
'Dewgong', 'Shellder', 'Cloyster', 'Krabby', 'Kingler', 'Horsea',
'Seadra', 'Goldeen', 'Seaking', 'Staryu', 'Starmie', 'Magikarp',
'Gyarados', 'Mega Gyarados', 'Lapras', 'Vaporeon'], (2, 'Bug'):
['Ledyba', 'Ledian', 'Spinarak', 'Ariados', 'Yanma', 'Pineco',
'Forretress', 'Scizor', 'Mega Scizor', 'Shuckle', 'Heracross', 'Mega
Heracross'], (2, 'Dark'): ['Umbreon', 'Murkrow', 'Sneasel',
'Houndour', 'Houndoom', 'Mega Houndoom'], (2, 'Electric'): ['Pichu',
'Mareep', 'Flaaffy', 'Ampharos', 'Mega Ampharos', 'Elekid', 'Raikou'],
(2, 'Fairy'): ['Cleffa', 'Togepi', 'Togetic', 'Snubbull', 'Granbull'],
(2, 'Fighting'): ['Tyrogue', 'Hitmontop'], (2, 'Fire'): ['Cyndaquil',
'Quilava', 'Typhlosion', 'Slugma', 'Magcargo', 'Magby', 'Entei', 'Ho-
oh'], (2, 'Ghost'): ['Misdreavus'], (2, 'Grass'): ['Chikorita',
'Bayleef', 'Meganium', 'Bellossom', 'Hoppip', 'Skiploom', 'Jumpluff',
'Sunkern', 'Sunflora'], (2, 'Ground'): ['Gligar', 'Phanpy',
'Donphan'], (2, 'Ice'): ['Swinub', 'Piloswine', 'Delibird',
'Smoochum'], (2, 'Normal'): ['Sentret', 'Furret', 'Hoothoot',
'Noctowl', 'Igglybuff', 'Aipom', 'Girafarig', 'Dunsparce',
'Teddiursa', 'Ursaring', 'Porygon2', 'Stantler', 'Smeargle',
'Miltank', 'Blissey'], (2, 'Poison'): ['Crobat'], (2, 'Psychic'):
['Natu', 'Xatu', 'Espeon', 'Unown', 'Wobbuffet', 'Lugia', 'Celebi'],
(2, 'Rock'): ['Sudowoodo', 'Larvitar', 'Pupitar', 'Tyranitar', 'Mega
Tyranitar'], (2, 'Steel'): ['Steelix', 'Mega Steelix', 'Skarmory'],
(2, 'Water'): ['Totodile', 'Croconaw', 'Feraligatr', 'Chinchou',
'Lanturn', 'Marill', 'Azumarill', 'Politoed', 'Wooper', 'Quagsire',
'Slowking', 'Qwilfish', 'Corsola', 'Remoraid', 'Octillery', 'Mantine',
'Kingdra', 'Suicune'], (3, 'Bug'): ['Wurmple', 'Silcoon', 'Beautifly',
'Cascoon', 'Dustox', 'Surskit', 'Masquerain', 'Nincada', 'Ninjask',
'Shedinja', 'Volbeat', 'Illumise'], (3, 'Dark'): ['Poochyena',
'Mightyena', 'Sableye', 'Mega Sableye', 'Absol', 'Mega Absol'], (3,
'Dragon'): ['Altaria', 'Mega Altaria', 'Bagon', 'Shelgon',
'Salamence', 'Mega Salamence', 'Latias', 'Mega Latias', 'Latios',
'Mega Latios', 'Rayquaza', 'Mega Rayquaza'], (3, 'Electric'):
['Electrike', 'Manectric', 'Mega Manectric', 'Plusle', 'Minun'], (3,
'Fighting'): ['Makuhita', 'Hariyama', 'Meditite', 'Medicham', 'Mega
Medicham'], (3, 'Fire'): ['Torchic', 'Combusken', 'Blaziken', 'Mega
Blaziken', 'Numel', 'Camerupt', 'Mega Camerupt', 'Torkoal'], (3,
'Ghost'): ['Shuppet', 'Banette', 'Mega Banette', 'Duskull',
'Dusclops'], (3, 'Grass'): ['Treecko', 'Grovyle', 'Sceptile', 'Mega
Sceptile', 'Seedot', 'Nuzleaf', 'Shiftry', 'Shroomish', 'Breloom',
'Roselia', 'Cacnea', 'Cacturne', 'Tropius'], (3, 'Ground'):
['Trapinch', 'Vibrava', 'Flygon', 'Baltoy', 'Claydol', 'Groudon',
'Primal Groudon'], (3, 'Ice'): ['Snorunt', 'Glalie', 'Mega Glalie',
'Spheal', 'Sealeo', 'Walrein', 'Regice'], (3, 'Normal'): ['Zigzagoon',
'Linoone', 'Taillow', 'Swellow', 'Slakoth', 'Vigoroth', 'Slaking',
'Whismur', 'Loudred', 'Exploud', 'Azurill', 'Skitty', 'Delcatty',
'Spinda', 'Swablu', 'Zangoose', 'Castform', 'Kecleon'], (3, 'Poison'):
['Gulpin', 'Swalot', 'Seviper'], (3, 'Psychic'): ['Ralts', 'Kirlia',
'Gardevoir', 'Mega Gardevoir', 'Spoink', 'Grumpig', 'Chimecho',
'Wynaut', 'Deoxys Normal Forme', 'DeoxysAttack Forme', 'Deoxys Defense
Forme', 'Deoxys Speed Forme'], (3, 'Rock'): ['Nosepass', 'Lunatone',
'Solrock', 'Lileep', 'Cradily', 'Anorith', 'Armaldo', 'Regirock'], (3,
'Steel'): ['Mawile', 'Mega Mawile', 'Aron', 'Lairon', 'Aggron', 'Mega
Aggron', 'Beldum', 'Metang', 'Metagross', 'Mega Metagross',
'Registeel', 'Jirachi'], (3, 'Water'): ['Mudkip', 'Marshtomp',
'Swampert', 'Mega Swampert', 'Lotad', 'Lombre', 'Ludicolo', 'Wingull',
'Pelipper', 'Carvanha', 'Sharpedo', 'Mega Sharpedo', 'Wailmer',
'Wailord', 'Barboach', 'Whiscash', 'Corphish', 'Crawdaunt', 'Feebas',
'Milotic', 'Clamperl', 'Huntail', 'Gorebyss', 'Relicanth', 'Luvdisc',
'Kyogre', 'Primal Kyogre'], (4, 'Bug'): ['Kricketot', 'Kricketune',
'Burmy', 'Wormadam Plant Cloak', 'Wormadam Sandy Cloak', 'Wormadam
Trash Cloak', 'Mothim', 'Combee', 'Vespiquen', 'Yanmega'], (4,
'Dark'): ['Honchkrow', 'Weavile', 'Darkrai'], (4, 'Dragon'): ['Gible',
'Gabite', 'Garchomp', 'Mega Garchomp'], (4, 'Electric'): ['Shinx',
'Luxio', 'Luxray', 'Pachirisu', 'Magnezone', 'Electivire', 'Rotom',
'Heat Rotom', 'Wash Rotom', 'Frost Rotom', 'Fan Rotom', 'Mow Rotom'],
(4, 'Fairy'): ['Togekiss'], (4, 'Fighting'): ['Riolu', 'Lucario',
'Mega Lucario'], (4, 'Fire'): ['Chimchar', 'Monferno', 'Infernape',
'Magmortar', 'Heatran'], (4, 'Ghost'): ['Drifloon', 'Drifblim',
'Mismagius', 'Spiritomb', 'Dusknoir', 'Giratina Altered Forme',
'Giratina Origin Forme'], (4, 'Grass'): ['Turtwig', 'Grotle',
'Torterra', 'Budew', 'Roserade', 'Cherubi', 'Cherrim', 'Carnivine',
'Snover', 'Abomasnow', 'Mega Abomasnow', 'Tangrowth', 'Leafeon',
'Shaymin Land Forme', 'Shaymin Sky Forme'], (4, 'Ground'):
['Hippopotas', 'Hippowdon', 'Rhyperior', 'Gliscor'], (4, 'Ice'):
['Glaceon', 'Mamoswine', 'Froslass'], (4, 'Normal'): ['Starly',
'Staravia', 'Staraptor', 'Bidoof', 'Bibarel', 'Ambipom', 'Buneary',
'Lopunny', 'Mega Lopunny', 'Glameow', 'Purugly', 'Happiny', 'Chatot',
'Munchlax', 'Lickilicky', 'Porygon-Z', 'Regigigas', 'Arceus'], (4,
'Poison'): ['Stunky', 'Skuntank', 'Skorupi', 'Drapion', 'Croagunk',
'Toxicroak'], (4, 'Psychic'): ['Chingling', 'Mime Jr.', 'Gallade',
'Mega Gallade', 'Uxie', 'Mesprit', 'Azelf', 'Cresselia'], (4, 'Rock'):
['Cranidos', 'Rampardos', 'Shieldon', 'Bastiodon', 'Bonsly',
'Probopass'], (4, 'Steel'): ['Bronzor', 'Bronzong', 'Dialga'], (4,
'Water'): ['Piplup', 'Prinplup', 'Empoleon', 'Buizel', 'Floatzel',
'Shellos', 'Gastrodon', 'Finneon', 'Lumineon', 'Mantyke', 'Palkia',
'Phione', 'Manaphy'], (5, 'Bug'): ['Sewaddle', 'Swadloon', 'Leavanny',
'Venipede', 'Whirlipede', 'Scolipede', 'Dwebble', 'Crustle',
'Karrablast', 'Escavalier', 'Joltik', 'Galvantula', 'Shelmet',
'Accelgor', 'Durant', 'Larvesta', 'Volcarona', 'Genesect'], (5,
'Dark'): ['Purrloin', 'Liepard', 'Scraggy', 'Scrafty', 'Zorua',
'Zoroark', 'Pawniard', 'Bisharp', 'Vullaby', 'Mandibuzz', 'Deino',
'Zweilous', 'Hydreigon'], (5, 'Dragon'): ['Axew', 'Fraxure',
'Haxorus', 'Druddigon', 'Reshiram', 'Zekrom', 'Kyurem', 'Kyurem Black
Kyurem', 'Kyurem White Kyurem'], (5, 'Electric'): ['Blitzle',
'Zebstrika', 'Emolga', 'Tynamo', 'Eelektrik', 'Eelektross', 'Thundurus
Incarnate Forme', 'Thundurus Therian Forme'], (5, 'Fighting'):
['Timburr', 'Gurdurr', 'Conkeldurr', 'Throh', 'Sawk', 'Mienfoo',
'Mienshao'], (5, 'Fire'): ['Tepig', 'Pignite', 'Emboar', 'Pansear',
'Simisear', 'Darumaka', 'Darmanitan Standard Mode', 'Darmanitan Zen
Mode', 'Heatmor'], (5, 'Flying'): ['Tornadus Incarnate Forme',
'Tornadus Therian Forme'], (5, 'Ghost'): ['Yamask', 'Cofagrigus',
'Litwick', 'Lampent', 'Chandelure'], (5, 'Grass'): ['Snivy',
'Servine', 'Serperior', 'Pansage', 'Simisage', 'Cottonee',
'Whimsicott', 'Petilil', 'Lilligant', 'Maractus', 'Foongus',
'Amoonguss', 'Ferroseed', 'Ferrothorn', 'Virizion'], (5, 'Ground'):
['Drilbur', 'Excadrill', 'Sandile', 'Krokorok', 'Krookodile',
'Stunfisk', 'Golett', 'Golurk', 'Landorus Incarnate Forme', 'Landorus
Therian Forme'], (5, 'Ice'): ['Vanillite', 'Vanillish', 'Vanilluxe',
'Cubchoo', 'Beartic', 'Cryogonal'], (5, 'Normal'): ['Patrat',
'Watchog', 'Lillipup', 'Herdier', 'Stoutland', 'Pidove', 'Tranquill',
'Unfezant', 'Audino', 'Mega Audino', 'Minccino', 'Cinccino',
'Deerling', 'Sawsbuck', 'Bouffalant', 'Rufflet', 'Braviary', 'Meloetta
Aria Forme', 'Meloetta Pirouette Forme'], (5, 'Poison'): ['Trubbish',
'Garbodor'], (5, 'Psychic'): ['Victini', 'Munna', 'Musharna',
'Woobat', 'Swoobat', 'Sigilyph', 'Gothita', 'Gothorita', 'Gothitelle',
'Solosis', 'Duosion', 'Reuniclus', 'Elgyem', 'Beheeyem'], (5, 'Rock'):
['Roggenrola', 'Boldore', 'Gigalith', 'Archen', 'Archeops',
'Terrakion'], (5, 'Steel'): ['Klink', 'Klang', 'Klinklang',
'Cobalion'], (5, 'Water'): ['Oshawott', 'Dewott', 'Samurott',
'Panpour', 'Simipour', 'Tympole', 'Palpitoad', 'Seismitoad',
'Basculin', 'Tirtouga', 'Carracosta', 'Ducklett', 'Swanna',
'Frillish', 'Jellicent', 'Alomomola', 'Keldeo Ordinary Forme', 'Keldeo
Resolute Forme'], (6, 'Bug'): ['Scatterbug', 'Spewpa', 'Vivillon'],
(6, 'Dark'): ['Inkay', 'Malamar', 'Yveltal'], (6, 'Dragon'): ['Goomy',
'Sliggoo', 'Goodra', 'Zygarde Half Forme'], (6, 'Electric'):
['Helioptile', 'Heliolisk', 'Dedenne'], (6, 'Fairy'): ['Flabébé',
'Floette', 'Florges', 'Spritzee', 'Aromatisse', 'Swirlix', 'Slurpuff',
'Sylveon', 'Xerneas'], (6, 'Fighting'): ['Pancham', 'Pangoro',
'Hawlucha'], (6, 'Fire'): ['Fennekin', 'Braixen', 'Delphox',
'Fletchinder', 'Talonflame', 'Litleo', 'Pyroar', 'Volcanion'], (6,
'Flying'): ['Noibat', 'Noivern'], (6, 'Ghost'): ['Phantump',
'Trevenant', 'Pumpkaboo Average Size', 'Pumpkaboo Small Size',
'Pumpkaboo Large Size', 'Pumpkaboo Super Size', 'Gourgeist Average
Size', 'Gourgeist Small Size', 'Gourgeist Large Size', 'Gourgeist
Super Size'], (6, 'Grass'): ['Chespin', 'Quilladin', 'Chesnaught',
'Skiddo', 'Gogoat'], (6, 'Ice'): ['Bergmite', 'Avalugg'], (6,
'Normal'): ['Bunnelby', 'Diggersby', 'Fletchling', 'Furfrou'], (6,
'Poison'): ['Skrelp', 'Dragalge'], (6, 'Psychic'): ['Espurr',
'Meowstic Male', 'Meowstic Female', 'Hoopa Confined', 'Hoopa
Unbound'], (6, 'Rock'): ['Binacle', 'Barbaracle', 'Tyrunt',
'Tyrantrum', 'Amaura', 'Aurorus', 'Carbink', 'Diancie', 'Mega
Diancie'], (6, 'Steel'): ['Honedge', 'Doublade', 'Aegislash Blade
Forme', 'Aegislash Shield Forme', 'Klefki'], (6, 'Water'): ['Froakie',
'Frogadier', 'Greninja', 'Clauncher', 'Clawitzer']}

Q. Group by Generation and get the mean value


of attack for each generation
df.groupby('Generation')['Attack'].mean() #aggregate function

Generation
1 76.638554
2 72.028302
3 81.625000
4 82.867769
5 82.066667
6 75.804878
Name: Attack, dtype: float64

df.groupby('Generation')['Sp. Atk'].median()

Generation
1 65.0
2 65.0
3 70.0
4 71.0
5 65.0
6 65.0
Name: Sp. Atk, dtype: float64

df.groupby('Generation')['Attack'].mean().sort_values(ascending=True)

Generation
2 72.028302
6 75.804878
1 76.638554
3 81.625000
5 82.066667
4 82.867769
Name: Attack, dtype: float64

Q. Find the fastest pokemon in type 1


df.groupby('Type 1')
['Speed'].max().sort_values(ascending=False).idxmax()

'Psychic'

Pivot table
pd.pivot_table(df,index='Generation',values='Sp. Atk',aggfunc='sum')

Sp. Atk
Generation
1 11922
2 6990
3 12129
4 9245
5 11878
6 6092

df.groupby('Type 1')
['Speed'].max().sort_values(ascending=False).idxmax()

'Psychic'

df['Attack'].unique()

array([ 49, 62, 82, 100, 52, 64, 84, 130, 104, 48, 63, 83,
103,
30, 20, 45, 35, 25, 90, 150, 60, 80, 56, 81, 85,
55,
75, 47, 92, 57, 72, 102, 70, 41, 76, 50, 65, 95,
105,
110, 40, 120, 73, 5, 125, 67, 155, 10, 115, 135, 134,
190,
46, 38, 58, 33, 185, 164, 160, 51, 71, 91, 140, 43,
78,
15, 165, 68, 23, 145, 180, 89, 109, 66, 86, 42, 29,
59,
79, 69, 94, 136, 93, 24, 170, 112, 61, 106, 132, 123,
88,
53, 98, 77, 27, 117, 108, 44, 87, 147, 74, 124, 97,
129,
128, 107, 36, 22, 54, 121, 131], dtype=int64)

def chk_attack(x):
if x<60:
return "Low attack"
elif x>60 and x<120:
return "Normal attack"
else:
return "High attack"
df['Attack']=df['Attack'].apply(chk_attack)
df

Type 1 Type 2 Health Points Attack Defense


\
Name

Bulbasaur Grass Poison 45 Low attack 49

Ivysaur Grass Poison 60 Normal attack 63

Venusaur Grass Poison 80 Normal attack 83

Mega Venusaur Grass Poison 80 Normal attack 123

Charmander Fire NaN 39 Low attack 43

... ... ... ... ... ...

Diancie Rock Fairy 50 Normal attack 150

Mega Diancie Rock Fairy 50 High attack 110

Hoopa Confined Psychic Ghost 80 Normal attack 60

Hoopa Unbound Psychic Dark 80 High attack 60

Volcanion Fire Water 80 Normal attack 120

Sp. Atk Sp. Def Speed Generation Legendary Total


Name

Bulbasaur 65 65 45 1 False 318

Ivysaur 80 80 60 1 False 405

Venusaur 100 100 80 1 False 525

Mega Venusaur 122 120 80 1 False 625

Charmander 60 50 65 1 False 309

... ... ... ... ... ... ...

Diancie 100 150 50 6 True 600

Mega Diancie 160 110 110 6 True 700

Hoopa Confined 150 130 70 6 True 600

Hoopa Unbound 170 130 80 6 True 680

Volcanion 130 90 70 6 True 600

[800 rows x 11 columns]

Merging()
df1 = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'],
'weight': ['high', 'medium', 'low'],
'price': np.random.randint(0, 15, 3)})

df2 = pd.DataFrame({'fruit': ['apple', 'orange', 'pine'],


'kilo': ['high', 'medium', 'low'],
'price': np.random.randint(0, 15, 3)})

df1

fruit weight price


0 apple high 5
1 banana medium 4
2 orange low 10

df2

fruit kilo price


0 apple high 13
1 orange medium 5
2 pine low 12
#inner
#outer
#left
#right

pd.merge(left=df1,right=df2,on='fruit',how='inner') #on-which
column, how-which type of merge #common

fruit weight price_x kilo price_y


0 apple high 5 high 13
1 orange low 10 medium 5

pd.merge(left=df1,right=df2,on='fruit',how='outer') # all the values

fruit weight price_x kilo price_y


0 apple high 5.0 high 13.0
1 banana medium 4.0 NaN NaN
2 orange low 10.0 medium 5.0
3 pine NaN NaN low 12.0

pd.merge(left=df1,right=df2,on='fruit',how='left') #only the fruits


in df1

fruit weight price_x kilo price_y


0 apple high 5 high 13.0
1 banana medium 4 NaN NaN
2 orange low 10 medium 5.0

pd.merge(left=df1,right=df2,on='fruit',how='right') #only the fruits


in df2

fruit weight price_x kilo price_y


0 apple high 5.0 high 13
1 orange low 10.0 medium 5
2 pine NaN NaN low 12

You might also like