本案例是Pandas数据分析课程【第二章】Series和DataFrame的配套案例。案例使用Pokemon宠物小精灵数据进行初步探索,演示第二章的知识点。
案例中使用宠物小精灵的相关数据进行分析,其中各列的列名意义为:
name
:宠物小精灵的名称Type 1
:宠物小精灵的第一类型Type 2
:宠物小精灵的第二类型HP
:生命点数Attack
:攻击强度Defense
:防御强度Sp.Atk
:特殊攻击强度Sp.Def
:特殊防御强度Speed
:速度Generation
:世代数Total
:综合能力(生命点数、攻击强度、防御强度、特殊攻击强度、特殊防御强度和速度的总和)Lengendary
:True表示为传奇小精灵,False表示非传奇小精灵首先调用Pandas和NumPy模块。
import pandas as pd
import numpy as np
使用read_csv
读取Pokemon.csv文件,存储在变量df
中,使用head
展示数据的前十行。
df=pd.read_csv('./input/Pokemon.csv')
df.head(10)
# | 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 |
5 | 5 | Charmeleon | Fire | NaN | 405 | 58 | 64 | 58 | 80 | 65 | 80 | 1 | False |
6 | 6 | Charizard | Fire | Flying | 534 | 78 | 84 | 78 | 109 | 85 | 100 | 1 | False |
7 | 6 | CharizardMega Charizard X | Fire | Dragon | 634 | 78 | 130 | 111 | 130 | 85 | 100 | 1 | False |
8 | 6 | CharizardMega Charizard Y | Fire | Flying | 634 | 78 | 104 | 78 | 159 | 115 | 100 | 1 | False |
9 | 7 | Squirtle | Water | NaN | 314 | 44 | 48 | 65 | 50 | 64 | 43 | 1 | False |
使用pd.set_option
设置只显示DataFrame最多10行样本,中间部分样本用省略号略过。
pd.set_option('display.max_rows',10)
df
# | 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
删除名为#
的列。
del df['#']
df
Name | Type 1 | Type 2 | Total | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Bulbasaur | Grass | Poison | 318 | 45 | 49 | 49 | 65 | 65 | 45 | 1 | False |
1 | Ivysaur | Grass | Poison | 405 | 60 | 62 | 63 | 80 | 80 | 60 | 1 | False |
2 | Venusaur | Grass | Poison | 525 | 80 | 82 | 83 | 100 | 100 | 80 | 1 | False |
3 | VenusaurMega Venusaur | Grass | Poison | 625 | 80 | 100 | 123 | 122 | 120 | 80 | 1 | False |
4 | Charmander | Fire | NaN | 309 | 39 | 52 | 43 | 60 | 50 | 65 | 1 | False |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
795 | Diancie | Rock | Fairy | 600 | 50 | 100 | 150 | 100 | 150 | 50 | 6 | True |
796 | DiancieMega Diancie | Rock | Fairy | 700 | 50 | 160 | 110 | 160 | 110 | 110 | 6 | True |
797 | HoopaHoopa Confined | Psychic | Ghost | 600 | 80 | 110 | 60 | 150 | 130 | 70 | 6 | True |
798 | HoopaHoopa Unbound | Psychic | Dark | 680 | 80 | 160 | 60 | 170 | 130 | 80 | 6 | True |
799 | Volcanion | Fire | Water | 600 | 80 | 110 | 120 | 130 | 90 | 70 | 6 | True |
800 rows × 12 columns
将各列名改为中文, 参数inplace
设置是否替换原数据。
new_name={'Name':'名称','Type 1':'第一类型','Type 2':'第二类型','HP':'生命点数','Attack':'攻击强度','Defense':'防御强度',
'Sp. Atk':'特殊攻击强度','Sp. Def':'特殊防御强度','Speed':'速度','Generation':'世代数','Total':'综合能力','Legendary':'传奇小精灵'}
df.rename(columns=new_name,inplace='True')
df
名称 | 第一类型 | 第二类型 | 综合能力 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Bulbasaur | Grass | Poison | 318 | 45 | 49 | 49 | 65 | 65 | 45 | 1 | False |
1 | Ivysaur | Grass | Poison | 405 | 60 | 62 | 63 | 80 | 80 | 60 | 1 | False |
2 | Venusaur | Grass | Poison | 525 | 80 | 82 | 83 | 100 | 100 | 80 | 1 | False |
3 | VenusaurMega Venusaur | Grass | Poison | 625 | 80 | 100 | 123 | 122 | 120 | 80 | 1 | False |
4 | Charmander | Fire | NaN | 309 | 39 | 52 | 43 | 60 | 50 | 65 | 1 | False |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
795 | Diancie | Rock | Fairy | 600 | 50 | 100 | 150 | 100 | 150 | 50 | 6 | True |
796 | DiancieMega Diancie | Rock | Fairy | 700 | 50 | 160 | 110 | 160 | 110 | 110 | 6 | True |
797 | HoopaHoopa Confined | Psychic | Ghost | 600 | 80 | 110 | 60 | 150 | 130 | 70 | 6 | True |
798 | HoopaHoopa Unbound | Psychic | Dark | 680 | 80 | 160 | 60 | 170 | 130 | 80 | 6 | True |
799 | Volcanion | Fire | Water | 600 | 80 | 110 | 120 | 130 | 90 | 70 | 6 | True |
800 rows × 12 columns
分别查看数据表的行、列索引。
df.index
RangeIndex(start=0, stop=800, step=1)
df.columns
Index(['名称', '第一类型', '第二类型', '综合能力', '生命点数', '攻击强度', '防御强度', '特殊攻击强度', '特殊防御强度', '速度', '世代数', '传奇小精灵'], dtype='object')
查看第一类型
的唯一值,有如下两种方法:
df['第一类型'].unique()
array(['Grass', 'Fire', 'Water', 'Bug', 'Normal', 'Poison', 'Electric', 'Ground', 'Fairy', 'Fighting', 'Psychic', 'Rock', 'Ghost', 'Ice', 'Dragon', 'Dark', 'Steel', 'Flying'], dtype=object)
df['第一类型'].value_counts()
Water 112 Normal 98 Grass 70 Bug 69 Psychic 57 ... Steel 27 Fighting 27 Ice 24 Fairy 17 Flying 4 Name: 第一类型, Length: 18, dtype: int64
可以用以下方式将中间省略部分全部显示出来。
pd.set_option('display.max_rows',None)
df['第一类型'].value_counts()
Water 112 Normal 98 Grass 70 Bug 69 Psychic 57 Fire 52 Electric 44 Rock 44 Ghost 32 Dragon 32 Ground 32 Dark 31 Poison 28 Steel 27 Fighting 27 Ice 24 Fairy 17 Flying 4 Name: 第一类型, dtype: int64
使用df.loc[label]
方法查看行标签为2、4小精灵的名称和综合能力。
df.loc[[2,4],['名称','综合能力']]
名称 | 综合能力 | |
---|---|---|
2 | Venusaur | 525 |
4 | Charmander | 309 |
使用df.iloc[loc]
方法查看行标签为5到10小精灵的名称和综合能力。
df.iloc[5:11,[0,3]]
名称 | 综合能力 | |
---|---|---|
5 | Charmeleon | 405 |
6 | Charizard | 534 |
7 | CharizardMega Charizard X | 634 |
8 | CharizardMega Charizard Y | 634 |
9 | Squirtle | 314 |
10 | Wartortle | 405 |
通过切片方式查看df
中前6个小精灵的数据。
df[:6]
名称 | 第一类型 | 第二类型 | 综合能力 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Bulbasaur | Grass | Poison | 318 | 45 | 49 | 49 | 65 | 65 | 45 | 1 | False |
1 | Ivysaur | Grass | Poison | 405 | 60 | 62 | 63 | 80 | 80 | 60 | 1 | False |
2 | Venusaur | Grass | Poison | 525 | 80 | 82 | 83 | 100 | 100 | 80 | 1 | False |
3 | VenusaurMega Venusaur | Grass | Poison | 625 | 80 | 100 | 123 | 122 | 120 | 80 | 1 | False |
4 | Charmander | Fire | NaN | 309 | 39 | 52 | 43 | 60 | 50 | 65 | 1 | False |
5 | Charmeleon | Fire | NaN | 405 | 58 | 64 | 58 | 80 | 65 | 80 | 1 | False |
查看攻击强度大于160的小精灵的所有数据。
df[df['攻击强度']>160]
名称 | 第一类型 | 第二类型 | 综合能力 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
163 | MewtwoMega Mewtwo X | Psychic | Fighting | 780 | 106 | 190 | 100 | 154 | 100 | 130 | 1 | True |
232 | HeracrossMega Heracross | Bug | Fighting | 600 | 80 | 185 | 115 | 40 | 105 | 75 | 2 | False |
268 | TyranitarMega Tyranitar | Rock | Dark | 700 | 100 | 164 | 150 | 95 | 120 | 71 | 2 | False |
387 | BanetteMega Banette | Ghost | NaN | 555 | 64 | 165 | 75 | 93 | 83 | 75 | 3 | False |
424 | GroudonPrimal Groudon | Ground | Fire | 770 | 100 | 180 | 160 | 150 | 90 | 90 | 3 | True |
426 | RayquazaMega Rayquaza | Dragon | Flying | 780 | 105 | 180 | 100 | 180 | 100 | 115 | 3 | True |
429 | DeoxysAttack Forme | Psychic | NaN | 600 | 50 | 180 | 20 | 180 | 20 | 150 | 3 | True |
454 | Rampardos | Rock | NaN | 495 | 97 | 165 | 60 | 65 | 50 | 58 | 4 | False |
494 | GarchompMega Garchomp | Dragon | Ground | 700 | 108 | 170 | 115 | 120 | 95 | 92 | 4 | False |
527 | GalladeMega Gallade | Psychic | Fighting | 618 | 68 | 165 | 95 | 65 | 115 | 110 | 4 | False |
711 | KyuremBlack Kyurem | Dragon | Ice | 700 | 125 | 170 | 100 | 120 | 90 | 95 | 5 | True |
查看传奇小精灵的数据,这里只展示前十行数据。
df[df['传奇小精灵']==True].head(10)
名称 | 第一类型 | 第二类型 | 综合能力 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
156 | Articuno | Ice | Flying | 580 | 90 | 85 | 100 | 95 | 125 | 85 | 1 | True |
157 | Zapdos | Electric | Flying | 580 | 90 | 90 | 85 | 125 | 90 | 100 | 1 | True |
158 | Moltres | Fire | Flying | 580 | 90 | 100 | 90 | 125 | 85 | 90 | 1 | True |
162 | Mewtwo | Psychic | NaN | 680 | 106 | 110 | 90 | 154 | 90 | 130 | 1 | True |
163 | MewtwoMega Mewtwo X | Psychic | Fighting | 780 | 106 | 190 | 100 | 154 | 100 | 130 | 1 | True |
164 | MewtwoMega Mewtwo Y | Psychic | NaN | 780 | 106 | 150 | 70 | 194 | 120 | 140 | 1 | True |
262 | Raikou | Electric | NaN | 580 | 90 | 85 | 75 | 115 | 100 | 115 | 2 | True |
263 | Entei | Fire | NaN | 580 | 115 | 115 | 85 | 90 | 75 | 100 | 2 | True |
264 | Suicune | Water | NaN | 580 | 100 | 75 | 115 | 90 | 115 | 85 | 2 | True |
269 | Lugia | Psychic | Flying | 680 | 106 | 90 | 130 | 90 | 154 | 110 | 2 | True |
创建新列能力600
,插入到综合能力
一列的后面,该列显示综合能力大于等于600的为True
,小于600为False
。
df.insert(4,'能力600',df['综合能力']>=600)
df.head(10)
名称 | 第一类型 | 第二类型 | 综合能力 | 能力600 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Bulbasaur | Grass | Poison | 318 | False | 45 | 49 | 49 | 65 | 65 | 45 | 1 | False |
1 | Ivysaur | Grass | Poison | 405 | False | 60 | 62 | 63 | 80 | 80 | 60 | 1 | False |
2 | Venusaur | Grass | Poison | 525 | False | 80 | 82 | 83 | 100 | 100 | 80 | 1 | False |
3 | VenusaurMega Venusaur | Grass | Poison | 625 | True | 80 | 100 | 123 | 122 | 120 | 80 | 1 | False |
4 | Charmander | Fire | NaN | 309 | False | 39 | 52 | 43 | 60 | 50 | 65 | 1 | False |
5 | Charmeleon | Fire | NaN | 405 | False | 58 | 64 | 58 | 80 | 65 | 80 | 1 | False |
6 | Charizard | Fire | Flying | 534 | False | 78 | 84 | 78 | 109 | 85 | 100 | 1 | False |
7 | CharizardMega Charizard X | Fire | Dragon | 634 | True | 78 | 130 | 111 | 130 | 85 | 100 | 1 | False |
8 | CharizardMega Charizard Y | Fire | Flying | 634 | True | 78 | 104 | 78 | 159 | 115 | 100 | 1 | False |
9 | Squirtle | Water | NaN | 314 | False | 44 | 48 | 65 | 50 | 64 | 43 | 1 | False |
创建新列世代456
,插入到df
的最后一列,该列显示世代数为4、5和6的小精灵为True, 其他为False,随机选取10行数据进行展示。
df['世代数456']=df['世代数'].isin([4,5,6])
df.sample(10)
名称 | 第一类型 | 第二类型 | 综合能力 | 能力600 | 生命点数 | 攻击强度 | 防御强度 | 特殊攻击强度 | 特殊防御强度 | 速度 | 世代数 | 传奇小精灵 | 世代数456 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
68 | Abra | Psychic | NaN | 310 | False | 25 | 20 | 15 | 105 | 55 | 90 | 1 | False | False |
736 | Pyroar | Fire | Normal | 507 | False | 86 | 68 | 72 | 109 | 66 | 106 | 6 | False | True |
401 | Clamperl | Water | NaN | 345 | False | 35 | 64 | 85 | 74 | 55 | 32 | 3 | False | False |
375 | Baltoy | Ground | Psychic | 300 | False | 40 | 40 | 55 | 40 | 70 | 55 | 3 | False | False |
240 | Corsola | Water | Rock | 380 | False | 55 | 55 | 85 | 65 | 85 | 35 | 2 | False | False |
487 | Mime Jr. | Psychic | Fairy | 310 | False | 20 | 25 | 45 | 70 | 90 | 60 | 4 | False | True |
390 | Tropius | Grass | Flying | 460 | False | 99 | 68 | 83 | 72 | 87 | 51 | 3 | False | False |
486 | Bonsly | Rock | NaN | 290 | False | 50 | 80 | 95 | 10 | 45 | 10 | 4 | False | True |
22 | Pidgeot | Normal | Flying | 479 | False | 83 | 80 | 75 | 70 | 70 | 101 | 1 | False | False |
697 | Larvesta | Bug | Fire | 360 | False | 55 | 85 | 55 | 50 | 55 | 60 | 5 | False | True |