lec 16 pandas_continue
lec 16 pandas_continue
import numpy as np
In [2]: df=pd.read_csv('D:\\AEGIS\\Python\\pokemon.csv')
In [3]: df
Mega
3 4 Grass Poison 80 100 123 122 120 80
Venusaur
... ... ... ... ... ... ... ... ... ... ...
Mega
796 797 Rock Fairy 50 160 110 160 110 110
Diancie
Hoopa
797 798 Psychic Ghost 80 110 60 150 130 70
Confined
Hoopa
798 799 Psychic Dark 80 160 60 170 130 80
Unbound
In [5]: df
Out[5]: Type Sp. Sp.
# Name Type 1 HP Attack Defense Speed G
2 Atk Def
Mega
3 4 Grass Poison 80 100 123 122 120 80
Venusaur
... ... ... ... ... ... ... ... ... ... ...
Mega
796 797 Rock Fairy 50 160 110 160 110 110
Diancie
Hoopa
797 798 Psychic Ghost 80 110 60 150 130 70
Confined
Hoopa
798 799 Psychic Dark 80 160 60 170 130 80
Unbound
In [6]: df.drop(['#'],inplace=True,axis=1)
In [8]: df=df.set_index('Name')
In [9]: df
Out[9]: Type Health Sp. Sp.
Type 1 Attack Defense Speed Gener
2 Points Atk Def
Name
Mega
Grass Poison 80 100 123 122 120 80
Venusaur
Mega
Rock Fairy 50 160 110 160 110 110
Diancie
Hoopa
Psychic Ghost 80 110 60 150 130 70
Confined
Hoopa
Psychic Dark 80 160 60 170 130 80
Unbound
Out[11]: 25
Out[12]: 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
Out[13]: 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()
In [14]: df['Type 1'].apply(lambda x:x.lower())
Out[14]: 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
In [15]: isinstance('abc',str)
Out[15]: True
In [16]: isinstance(10,str)
Out[16]: False
In [17]: isinstance(10,int)
Out[17]: True
Out[18]: 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
Out[19]: 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()
In [20]: df.head()
Out[20]: Type Type Health Sp. Sp.
Attack Defense Speed Generat
1 2 Points Atk Def
Name
Mega
Grass Poison 80 100 123 122 120 80
Venusaur
In [22]: df.groupby('Generation').groups
Out[22]: {1: ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Mega Venusaur', 'Charmander', 'Ch
armeleon', 'Charizard', 'Mega Charizard X', 'Mega Charizard Y', 'Squirtle',
'Wartortle', 'Blastoise', 'Mega Blastoise', 'Caterpie', 'Metapod', 'Butterf
ree', 'Weedle', 'Kakuna', 'Beedrill', 'Mega Beedrill', 'Pidgey', 'Pidgeott
o', 'Pidgeot', 'Mega Pidgeot', 'Rattata', 'Raticate', 'Spearow', 'Fearow',
'Ekans', 'Arbok', 'Pikachu', 'Raichu', 'Sandshrew', 'Sandslash', 'Nidoran
♀', 'Nidorina', 'Nidoqueen', 'Nidoran♂', 'Nidorino', 'Nidoking', 'Clefair
y', 'Clefable', 'Vulpix', 'Ninetales', 'Jigglypuff', 'Wigglytuff', 'Zubat',
'Golbat', 'Oddish', 'Gloom', 'Vileplume', 'Paras', 'Parasect', 'Venonat',
'Venomoth', 'Diglett', 'Dugtrio', 'Meowth', 'Persian', 'Psyduck', 'Golduc
k', 'Mankey', nan, 'Growlithe', 'Arcanine', 'Poliwag', 'Poliwhirl', 'Poliwr
ath', 'Abra', 'Kadabra', 'Alakazam', 'Mega Alakazam', 'Machop', 'Machoke',
'Machamp', 'Bellsprout', 'Weepinbell', 'Victreebel', 'Tentacool', 'Tentacru
el', 'Geodude', 'Graveler', 'Golem', 'Ponyta', 'Rapidash', 'Slowpoke', 'Slo
wbro', 'Mega Slowbro', 'Magnemite', 'Magneton', 'Farfetch'd', 'Doduo', 'Dod
rio', 'Seel', 'Dewgong', 'Grimer', 'Muk', 'Shellder', 'Cloyster', 'Gastly',
...], 2: ['Chikorita', 'Bayleef', 'Meganium', 'Cyndaquil', 'Quilava', 'Typh
losion', 'Totodile', 'Croconaw', 'Feraligatr', 'Sentret', 'Furret', 'Hootho
ot', 'Noctowl', 'Ledyba', 'Ledian', 'Spinarak', 'Ariados', 'Crobat', 'Chinc
hou', 'Lanturn', 'Pichu', 'Cleffa', 'Igglybuff', 'Togepi', 'Togetic', 'Nat
u', 'Xatu', 'Mareep', 'Flaaffy', 'Ampharos', 'Mega Ampharos', 'Bellossom',
'Marill', 'Azumarill', 'Sudowoodo', 'Politoed', 'Hoppip', 'Skiploom', 'Jump
luff', 'Aipom', 'Sunkern', 'Sunflora', 'Yanma', 'Wooper', 'Quagsire', 'Espe
on', 'Umbreon', 'Murkrow', 'Slowking', 'Misdreavus', 'Unown', 'Wobbuffet',
'Girafarig', 'Pineco', 'Forretress', 'Dunsparce', 'Gligar', 'Steelix', 'Meg
a Steelix', 'Snubbull', 'Granbull', 'Qwilfish', 'Scizor', 'Mega Scizor', 'S
huckle', 'Heracross', 'Mega Heracross', 'Sneasel', 'Teddiursa', 'Ursaring',
'Slugma', 'Magcargo', 'Swinub', 'Piloswine', 'Corsola', 'Remoraid', 'Octill
ery', 'Delibird', 'Mantine', 'Skarmory', 'Houndour', 'Houndoom', 'Mega Houn
doom', 'Kingdra', 'Phanpy', 'Donphan', 'Porygon2', 'Stantler', 'Smeargle',
'Tyrogue', 'Hitmontop', 'Smoochum', 'Elekid', 'Magby', 'Miltank', 'Blisse
y', 'Raikou', 'Entei', 'Suicune', 'Larvitar', ...], 3: ['Treecko', 'Grovyl
e', 'Sceptile', 'Mega Sceptile', 'Torchic', 'Combusken', 'Blaziken', 'Mega
Blaziken', 'Mudkip', 'Marshtomp', 'Swampert', 'Mega Swampert', 'Poochyena',
'Mightyena', 'Zigzagoon', 'Linoone', 'Wurmple', 'Silcoon', 'Beautifly', 'Ca
scoon', 'Dustox', 'Lotad', 'Lombre', 'Ludicolo', 'Seedot', 'Nuzleaf', 'Shif
try', 'Taillow', 'Swellow', 'Wingull', 'Pelipper', 'Ralts', 'Kirlia', 'Gard
evoir', 'Mega Gardevoir', 'Surskit', 'Masquerain', 'Shroomish', 'Breloom',
'Slakoth', 'Vigoroth', 'Slaking', 'Nincada', 'Ninjask', 'Shedinja', 'Whismu
r', 'Loudred', 'Exploud', 'Makuhita', 'Hariyama', 'Azurill', 'Nosepass', 'S
kitty', 'Delcatty', 'Sableye', 'Mega Sableye', 'Mawile', 'Mega Mawile', 'Ar
on', 'Lairon', 'Aggron', 'Mega Aggron', 'Meditite', 'Medicham', 'Mega Medic
ham', 'Electrike', 'Manectric', 'Mega Manectric', 'Plusle', 'Minun', 'Volbe
at', 'Illumise', 'Roselia', 'Gulpin', 'Swalot', 'Carvanha', 'Sharpedo', 'Me
ga Sharpedo', 'Wailmer', 'Wailord', 'Numel', 'Camerupt', 'Mega Camerupt',
'Torkoal', 'Spoink', 'Grumpig', 'Spinda', 'Trapinch', 'Vibrava', 'Flygon',
'Cacnea', 'Cacturne', 'Swablu', 'Altaria', 'Mega Altaria', 'Zangoose', 'Sev
iper', 'Lunatone', 'Solrock', 'Barboach', ...], 4: ['Turtwig', 'Grotle', 'T
orterra', 'Chimchar', 'Monferno', 'Infernape', 'Piplup', 'Prinplup', 'Empol
eon', 'Starly', 'Staravia', 'Staraptor', 'Bidoof', 'Bibarel', 'Kricketot',
'Kricketune', 'Shinx', 'Luxio', 'Luxray', 'Budew', 'Roserade', 'Cranidos',
'Rampardos', 'Shieldon', 'Bastiodon', 'Burmy', 'Wormadam Plant Cloak', 'Wor
madam Sandy Cloak', 'Wormadam Trash Cloak', 'Mothim', 'Combee', 'Vespique
n', 'Pachirisu', 'Buizel', 'Floatzel', 'Cherubi', 'Cherrim', 'Shellos', 'Ga
strodon', 'Ambipom', 'Drifloon', 'Drifblim', 'Buneary', 'Lopunny', 'Mega Lo
punny', 'Mismagius', 'Honchkrow', 'Glameow', 'Purugly', 'Chingling', 'Stunk
y', 'Skuntank', 'Bronzor', 'Bronzong', 'Bonsly', 'Mime Jr.', 'Happiny', 'Ch
atot', 'Spiritomb', 'Gible', 'Gabite', 'Garchomp', 'Mega Garchomp', 'Munchl
ax', 'Riolu', 'Lucario', 'Mega Lucario', 'Hippopotas', 'Hippowdon', 'Skorup
i', 'Drapion', 'Croagunk', 'Toxicroak', 'Carnivine', 'Finneon', 'Lumineon',
'Mantyke', 'Snover', 'Abomasnow', 'Mega Abomasnow', 'Weavile', 'Magnezone',
'Lickilicky', 'Rhyperior', 'Tangrowth', 'Electivire', 'Magmortar', 'Togekis
s', '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', 'Lillipu
p', 'Herdier', 'Stoutland', 'Purrloin', 'Liepard', 'Pansage', 'Simisage',
'Pansear', 'Simisear', 'Panpour', 'Simipour', 'Munna', 'Musharna', 'Pidov
e', 'Tranquill', 'Unfezant', 'Blitzle', 'Zebstrika', 'Roggenrola', 'Boldor
e', 'Gigalith', 'Woobat', 'Swoobat', 'Drilbur', 'Excadrill', 'Audino', 'Meg
a Audino', 'Timburr', 'Gurdurr', 'Conkeldurr', 'Tympole', 'Palpitoad', 'Sei
smitoad', 'Throh', 'Sawk', 'Sewaddle', 'Swadloon', 'Leavanny', 'Venipede',
'Whirlipede', 'Scolipede', 'Cottonee', 'Whimsicott', 'Petilil', 'Lilligan
t', 'Basculin', 'Sandile', 'Krokorok', 'Krookodile', 'Darumaka', 'Darmanita
n Standard Mode', 'Darmanitan Zen Mode', 'Maractus', 'Dwebble', 'Crustle',
'Scraggy', 'Scrafty', 'Sigilyph', 'Yamask', 'Cofagrigus', 'Tirtouga', 'Carr
acosta', 'Archen', 'Archeops', 'Trubbish', 'Garbodor', 'Zorua', 'Zoroark',
'Minccino', 'Cinccino', 'Gothita', 'Gothorita', 'Gothitelle', 'Solosis', 'D
uosion', 'Reuniclus', 'Ducklett', 'Swanna', 'Vanillite', 'Vanillish', 'Vani
lluxe', 'Deerling', 'Sawsbuck', 'Emolga', 'Karrablast', 'Escavalier', 'Foon
gus', 'Amoonguss', ...], 6: ['Chespin', 'Quilladin', 'Chesnaught', 'Fenneki
n', 'Braixen', 'Delphox', 'Froakie', 'Frogadier', 'Greninja', 'Bunnelby',
'Diggersby', 'Fletchling', 'Fletchinder', 'Talonflame', 'Scatterbug', 'Spew
pa', 'Vivillon', 'Litleo', 'Pyroar', 'Flabébé', 'Floette', 'Florges', 'Skid
do', 'Gogoat', 'Pancham', 'Pangoro', 'Furfrou', 'Espurr', 'Meowstic Male',
'Meowstic Female', 'Honedge', 'Doublade', 'Aegislash Blade Forme', 'Aegisla
sh Shield Forme', 'Spritzee', 'Aromatisse', 'Swirlix', 'Slurpuff', 'Inkay',
'Malamar', 'Binacle', 'Barbaracle', 'Skrelp', 'Dragalge', 'Clauncher', 'Cla
witzer', 'Helioptile', 'Heliolisk', 'Tyrunt', 'Tyrantrum', 'Amaura', 'Auror
us', 'Sylveon', 'Hawlucha', 'Dedenne', 'Carbink', 'Goomy', 'Sliggoo', 'Good
ra', 'Klefki', 'Phantump', 'Trevenant', 'Pumpkaboo Average Size', 'Pumpkabo
o Small Size', 'Pumpkaboo Large Size', 'Pumpkaboo Super Size', 'Gourgeist A
verage Size', 'Gourgeist Small Size', 'Gourgeist Large Size', 'Gourgeist Su
per Size', 'Bergmite', 'Avalugg', 'Noibat', 'Noivern', 'Xerneas', 'Yvelta
l', 'Zygarde Half Forme', 'Diancie', 'Mega Diancie', 'Hoopa Confined', 'Hoo
pa Unbound', 'Volcanion']}
Out[25]: Generation
1 65.0
2 65.0
3 70.0
4 71.0
5 65.0
6 65.0
Name: Sp. Atk, dtype: float64
In [26]: df.groupby('Generation')['Attack'].mean().sort_values(ascending=True)
Out[26]: Generation
2 72.028302
6 75.804878
1 76.638554
3 81.625000
5 82.066667
4 82.867769
Name: Attack, dtype: float64
Out[27]: 'Psychic'
Pivot table
In [28]: pd.pivot_table(df,index='Generation',values='Sp. Atk',aggfunc='sum')
Out[28]: Sp. Atk
Generation
1 11922
2 6990
3 12129
4 9245
5 11878
6 6092
Out[29]: 'Psychic'
In [30]: df['Attack'].unique()
Out[30]: 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)
Name
Low
Bulbasaur Grass Poison 45 49 65 65 45
attack
Normal
Ivysaur Grass Poison 60 63 80 80 60
attack
Normal
Venusaur Grass Poison 80 83 100 100 80
attack
Mega Normal
Grass Poison 80 123 122 120 80
Venusaur attack
Low
Charmander Fire NaN 39 43 60 50 65
attack
Normal
Diancie Rock Fairy 50 150 100 150 50
attack
Mega High
Rock Fairy 50 110 160 110 110
Diancie attack
Hoopa Normal
Psychic Ghost 80 60 150 130 70
Confined attack
Hoopa High
Psychic Dark 80 60 170 130 80
Unbound attack
Normal
Volcanion Fire Water 80 120 130 90 70
attack
Merging()
In [32]: df1 = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'],
'weight': ['high', 'medium', 'low'],
'price': np.random.randint(0, 15, 3)})
In [33]: df1
Out[33]: fruit weight price
0 apple high 5
1 banana medium 4
2 orange low 10
In [34]: df2
0 apple high 13
1 orange medium 5
2 pine low 12
In [35]: #inner
#outer
#left
#right