索引总结
习题
现有一份关于口袋妖怪的数据集,请解决下列问题:
1.7.2.5 (a)双属性的Pokemon占总体比例的多少?
1.7.2.6 (b)在所有种族值(Total)不小于580的Pokemon中,非神兽(Legendary=False)的比例为多少?
1.7.2.7 (c)在第一属性为格斗系(Fighting)的Pokemon中,物攻排名前三高的是哪些?
1.7.2.8 (d)请问六项种族指标(HP、物攻、特攻、物防、特防、速度)极差的均值最大的是哪个属性(只考虑第一属性,且均值是对属性而言)?
1.7.2.9 (e)哪个属性(只考虑第一属性)的神兽比例最高?该属性神兽的种族值也是最高的吗?
#(a)
df = pd.read_csv('data/Pokemon.csv')
df['Type 2'].count()/df.shape[0]
0.5175
#(b)
df[df['Total']>=580][df['Legendary']==False].shape[0] / df[df['Total']>=580].shape[0] #方式1
df.query('Total >= 580')['Legendary'].value_counts(normalize=True) #方式2,用query方法较简洁
True 0.575221
False 0.424779
Name: Legendary, dtype: float64
#(c)
df[df['Type 1']=='Fighting'].sort_values(by='Attack',ascending=False).iloc[:3]
#(d)
df['range'] = df.iloc[:,5:11].max(axis=1)-df.iloc[:,5:11].min(axis=1)
attribute = df[['Type 1','range']].set_index('Type 1')# 将Type1设为index
max_range = 0
result = ''
for i in attribute.index.unique(): #遍历所有Type1
temp = attribute.loc[i,:].mean() #计算均值
if temp.values[0] > max_range: #比较
max_range = temp.values[0]
result = i
result
#(e)
df.query('Legendary == True')['Type 1'].value_counts(normalize=True)
Psychic 0.215385
Dragon 0.184615
Fire 0.076923
Electric 0.061538
Ground 0.061538
Water 0.061538
Steel 0.061538
Rock 0.061538
Grass 0.046154
Ghost 0.030769
Ice 0.030769
Normal 0.030769
Flying 0.030769
Dark 0.030769
Fairy 0.015385
Name: Type 1, dtype: float64