Lec 16 Pandas - Continue
Lec 16 Pandas - Continue
import numpy as np
df=pd.read_csv('D:\\AEGIS\\Python\\pokemon.csv')
df
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
df.drop(['#'],inplace=True,axis=1)
df.rename(columns={'HP':'Health Points'},inplace=True)
df=df.set_index('Name')
df
25
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
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()
df.groupby('Generation').groups
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']}
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
'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
Merging()
df1 = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'],
'weight': ['high', 'medium', 'low'],
'price': np.random.randint(0, 15, 3)})
df1
df2
pd.merge(left=df1,right=df2,on='fruit',how='inner') #on-which
column, how-which type of merge #common