In: Computer Science
Python problem
I don't know how to arrive to the answer of this question:
"Rounded to the nearest integer, how much higher is the average sum of all six stats among Mega Pokemon than their non-Mega versions? Note that Mega Pokemon share the same Number (the first column) as their non-Mega versions, which will allow you to find all Pokemon that have a Mega version."
For this problem below. The format of the file is included at the bottom but it's not complete. If you want the complete one, let me know and I can email it to you.
#The line below will open a file containing information
#about every pokemon through Generation 7:
pokedex = open('../resource/lib/public/pokedex.csv', 'r')
#We've also provided a sample subset of the data in
#sample.csv.
#
#Each line of the file has 13 values, separated by commas.
#They are:
#
#
# - Number: The numbered ID of the Pokemon, an integer
# - Name: The name of the Pokemon, a string
# - Type1: The Pokemon's primary type, a string
# - Type2: The Pokemon's secondary type, a string (this
# may be blank)
# - HP: The Pokemon's HP statistic, an integer in the range
# 1 to 255
# - Attack: The Pokemon's Attack statistic, an integer in
# the range 1 to 255
# - Defense: The Pokemon's Defense statistic, an integer in
# the range 1 to 255
# - SpecialAtk: The Pokemon's Special Attack statistic, an
# integer in the range 1 to 255
# - SpecialDef: The Pokemon's Special Defense statistic, an
# integer in the range 1 to 255
# - Speed: The Pokemon's Speed statistic, an integer in the
# range 1 to 255
# - Generation: What generation the Pokemon debuted in, an
# integer in the range 1 to 7
# - Legendary: Whether the Pokemon is considered "legendary"
# or not, either TRUE or FALSE
# - Mega: Whether the Pokemon is "Mega" or not, either TRUE
# or FALSE
#
#Use this dataset to answer the questions below.
#Here, add any code you want to allow you to answer the
#questions asked below over on edX. This is just a sandbox
#for you to explore the dataset: nothing is required for
#submission here.
Number,Name,Type1,Type2,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed,Generation,Legendary,Mega
1,Bulbasaur,Grass,Poison,45,49,49,65,65,45,1,FALSE,FALSE
2,Ivysaur,Grass,Poison,60,62,63,80,80,60,1,FALSE,FALSE
3,Venusaur,Grass,Poison,80,82,83,100,100,80,1,FALSE,FALSE
3,Mega
Venusaur,Grass,Poison,80,100,123,122,120,80,1,FALSE,TRUE
4,Charmander,Fire,,39,52,43,60,50,65,1,FALSE,FALSE
5,Charmeleon,Fire,,58,64,58,80,65,80,1,FALSE,FALSE
6,Charizard,Fire,Flying,78,84,78,109,85,100,1,FALSE,FALSE
6,Mega Charizard
X,Fire,Dragon,78,130,111,130,85,100,1,FALSE,TRUE
6,Mega Charizard
Y,Fire,Flying,78,104,78,159,115,100,1,FALSE,TRUE
7,Squirtle,Water,,44,48,65,50,64,43,1,FALSE,FALSE
8,Wartortle,Water,,59,63,80,65,80,58,1,FALSE,FALSE
9,Blastoise,Water,,79,83,100,85,105,78,1,FALSE,FALSE
9,Mega Blastoise,Water,,79,103,120,135,115,78,1,FALSE,TRUE
10,Caterpie,Bug,,45,30,35,20,20,45,1,FALSE,FALSE
11,Metapod,Bug,,50,20,55,25,25,30,1,FALSE,FALSE
12,Butterfree,Bug,Flying,60,45,50,90,80,70,1,FALSE,FALSE
13,Weedle,Bug,Poison,40,35,30,20,20,50,1,FALSE,FALSE
14,Kakuna,Bug,Poison,45,25,50,25,25,35,1,FALSE,FALSE
15,Beedrill,Bug,Poison,65,90,40,45,80,75,1,FALSE,FALSE
15,Mega Beedrill,Bug,Poison,65,150,40,15,80,145,1,FALSE,TRUE
16,Pidgey,Normal,Flying,40,45,40,35,35,56,1,FALSE,FALSE
17,Pidgeotto,Normal,Flying,63,60,55,50,50,71,1,FALSE,FALSE
18,Pidgeot,Normal,Flying,83,80,75,70,70,101,1,FALSE,FALSE
18,Mega
Pidgeot,Normal,Flying,83,80,80,135,80,121,1,FALSE,TRUE
19,Rattata,Normal,,30,56,35,25,35,72,1,FALSE,FALSE
20,Raticate,Normal,,55,81,60,50,70,97,1,FALSE,FALSE
Here is the code:
# selecting Mega Pokemon
mega_pokemon = pokedex[pokedex['Mega'] == 1]
mega_pokemon = mega_pokemon.reset_index(drop = True)
print('Total number: ',len(mega_pokemon))
mega_pokemon
# selecting nonMega Pokemon
nonMega_pokemon = pokedex[pokedex['Mega'] == 0]
nonMega_pokemon = nonMega_pokemon.reset_index(drop = True)
print('Total number: ',len(nonMega_pokemon))
# selecting common rows based on Number
common_number = list(np.intersect1d(mega_pokemon.Number, nonMega_pokemon.Number))
# selecting index based on common_number
indices = list()
for loop1 in range(len(nonMega_pokemon)):
for loop2 in range(len(mega_pokemon)):
if(nonMega_pokemon.iloc[loop1][0] == mega_pokemon.iloc[loop2][0]):
indices.append(loop1)
indices = list(set(indices))
# selecting rows based on indices
nonMega_pokemon = nonMega_pokemon.loc[indices]
nonMega_pokemon.head()
# Since with number 6, only one Pokemon is available in one group, removing other 6th Number
mega_pokemon = mega_pokemon.drop_duplicates(subset=['Number']) # removing duplicate on Number
mega_pokemon
def avg_sum_stats(dataframe): # calculate average sum of all the six stats
average_sum = dataframe.iloc[:,4:10]
average_sum = list((dataframe.sum(axis=1)) / len(xyz))
average_sum = [int(item+0.5) for item in xyz]
return(average_sum)
avg_sum1 = avg_sum_stats(mega_pokemon)
avg_sum2 = avg_sum_stats(nonMega_pokemon)
differences = [x1 - x2 for (x1, x2) in zip(avg_sum1, avg_sum2)]
differences
Here is the results:
Data:
NonMega Pokemon:
Mega Pokemon
Currently, with the given data, both the types of pokemon are same strength values of stats.