Pokémon trainers love data almost as much as they love their team. In this exercise you’ll analyse a CSV file of Pokémon stats with pandas and visualise the strongest ones with matplotlib.
You receive four CSV files in your working directory. Each row describes one Pokémon with the following columns:
| Column | Description |
|---|---|
name | Name of the Pokémon |
type1 | Primary type (e.g. Fire, Water, Grass) |
type2 | Secondary type, may be empty |
hp | Hit points |
attack | Physical attack |
defense | Physical defense |
sp_attack | Special attack |
sp_defense | Special defense |
speed | Speed |
The total stats of a Pokémon are the sum of its six numeric stats (hp + attack + defense + sp_attack + sp_defense + speed).
The four files are:
pokemon.csv — 20 Pokémon from various typesstarters.csv — the nine Generation 1 starter evolutionseeveelutions.csv — Eevee and three of its evolutionslegendary.csv — just one legendary PokémonThe editor is pre-filled with stubs for every function you have to implement and a small if __name__ == "sandbox": block that calls them on pokemon.csv and shows the chart. Replace each raise NotImplementedError with your implementation — pressing Run then prints the textual results and renders the bar chart in the sandbox. The if __name__ == "sandbox": guard makes the block fire only when you press Run; it is skipped during automated testing.
You may import pandas and matplotlib.pyplot.
load_pokemon(filename)Read the given CSV file and return the resulting pandas.DataFrame. The other functions can rely on this helper.
number_of_pokemon(filename)Return the number of Pokémon in the file (an int).
>>> number_of_pokemon('pokemon.csv')
20
>>> number_of_pokemon('legendary.csv')
1
all_types(filename)Return the sorted list of unique primary types (type1) that occur in the file.
>>> all_types('starters.csv')
['Fire', 'Grass', 'Water']
>>> all_types('legendary.csv')
['Psychic']
strongest_pokemon(filename)Return the name of the Pokémon with the highest total stats. When several Pokémon are tied for the highest total, return the one that appears first in the file.
>>> strongest_pokemon('pokemon.csv')
'Mewtwo'
>>> strongest_pokemon('starters.csv')
'Charizard'
strongest_per_type(filename)Return a dictionary that maps each primary type to the name of its strongest Pokémon (the one with the highest total stats within that type). When several Pokémon of the same type are tied for the highest total, take the one that appears first in the file.
>>> strongest_per_type('starters.csv')
{'Fire': 'Charizard', 'Grass': 'Venusaur', 'Water': 'Blastoise'}
plot_top_pokemon(filename, n)Build a matplotlib bar chart of the top n Pokémon by total stats (highest first) and return the resulting Figure. Each bar’s label is the Pokémon name and its height is the total stats. The chart must have:
Top {n} Pokémon by total stats (with {n} filled in)PokémonTotal statsimport matplotlib.pyplot as plt
fig = plot_top_pokemon('starters.csv', 3)
plt.show() # shows the chart
The bars must appear sorted from highest to lowest total — when several Pokémon are tied, those that appear first in the file go first.
Only the title, axis labels, bar count, heights and order are checked, so you’re free to style the chart however you like. The example solution colours each bar by the Pokémon’s primary type and labels it with its total.
When you submit, the judge captures the figure you return and renders it straight into the feedback, so you can see exactly what your chart looks like.