Useful CSE 163 Resources¶
Learning objective: Read, process, and group CSV data to compute descriptive statistics with and without Pandas.
You can find the starter code here. Make sure to extract (unzip) the contents anywhere on your computer. If you are working in VS Code, navigate to File | Open or Open Folder, then select the hw2 folder.
-
hw2_manual.pyis the file for you to put your implementations for solving each problem without Pandas. -
hw2_pandas.pyis the file for you to put your implementations for solving each problem with Pandas. -
hw2_test.pyis the file for you to put your own tests. The Run button executes this program. -
cse163_utils.pyis a helper file that has code to help you test your code. -
pokemon_box.csvis a large CSV file that stores information about pokemon. -
pokemon_test.csvis a very small CSV file that stores information about pokemon used for the example cases. -
PandasWordbank.ipynbis a Jupyter Notebook that reviews all of the Pandas features needed for this assessment. Feel free to edit this Jupyter Notebook to prototype ideas and explore the data. -
pets.csvis the tiny example dataset used only by thePandasWordbank.ipynb.
Context¶
In the Pokémon video game series, the player catches pokemon, fictional creatures trained to battle each other as part of a sport franchise. Pokémon exerted significant cultural influence on people who grew up in the late 1990s and early 2000s not only in its country of origin, Japan, but also around the world. More recently, Pokémon Go became a viral hit as hundreds of millions of people played the augmented-reality game at its peak during the summer of 2016. You do not need to understand the details of Pokémon or need to have played the game to do the assignment. All you need to understand is the statistics we provide in our dataset about each pokemon.
The pokemon_box.csv file stores some imagined data about a player’s pokemon in the following format.
| id | name | level | personality | type | weakness | atk | def | hp | stage |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Bulbasaur | 12 | Jolly | Grass | Fire | 45 | 50 | 112 | 1 |
-
idis a unique numeric identifier corresponding to the species of a pokemon. All pokemon of the same species share the sameid. -
nameis the name of the species of pokemon, such as Bulbasaur. -
levelis the integer level of the pokemon. -
personalityis a one-word string describing the personality of the pokemon, such as Jolly. -
typeis a one-word string describing the type of the pokemon, such as Grass. -
weaknessis the enemy type that this pokemon is weak toward. Bulbasaur is weak to fire-type pokemon. -
atk,def,hpare integers that indicate the attack power, defense power, and hit points of the pokemon. -
stageis an integer that indicates the particular developmental stage of the pokemon.
In the Charmander species, Charmander begins at stage 1, evolves into a Charmeleon at stage 2, and finally evolves into Charizard at stage 3.

The problems in this assessment focus on providing descriptive statistics for summarizing the pokemon dataset, such as computing the mean or count of a certain column. Solve each problem in two ways.
-
In
hw2_manual.py, solve each problem without the Pandas library by utilizing only built-in data structures and Python math functions. Data is given to each method as a list of dictionaries. -
In
hw2_pandas.py, solve each problem with the Pandas library (as well as some built-in data structures and Python math functions as necessary). Data is given as a PandasDataFrame.
Here are some examples about how to show how these functions can be called– you don’t need to add them to your files:
Manual functions:
data = parse('pokemon_box.csv')
print('Number of species:', hw2_manual.species_count(data))
print('Highest level pokemon:', hw2_manual.max_level(data))
print('Low-level Pokemon:', hw2_manual.filter_range(data, 1, 9))
print('Average attack for fire types:', hw2_manual.mean_attack_for_type(data, 'fire'))
print('Count of each Pokemon type:')
print(hw2_manual.count_types(data))
print('Average attack for each Pokemon type:')
print(hw2_manual.mean_attack_per_type(data))
data = pd.read_csv('pokemon_box.csv')
print('Number of species:', hw2_pandas.species_count(data))
print('Highest level pokemon:', hw2_pandas.max_level(data))
print('Low-level Pokemon:', hw2_pandas.filter_range(data, 1, 9))
print('Average attack for fire types:', hw2_pandas.mean_attack_for_type(data, 'fire'))
print('Count of each Pokemon type:')
print(hw2_pandas.count_types(data))
print('Average attack for each Pokemon type:')
print(hw2_pandas.mean_attack_per_type(data))
Assume the data is never empty (there’s at least one pokemon) and that there’s no missing data (each pokemon has every attribute).
Note
Canonically, Pokémon cannot have an attack stat of 0. For this assessment, however, you should NOT make that assumption.
Hint
Remember to use the PandasWordbank.ipynb Jupyter Notebook to explore the data and work in a more interactive environment. You might even prefer writing all of your code in the notebook and periodically transferring functions over to hw2.py to run the automated checks.
As with previous assessments, you’ll also check your solutions by adding tests to hw2_test.py using the assert_equals function.
-
To help test functions solved without Pandas,
cse163_utils.pydefines aparsefunction that takes a filename and returns the dataset as a list of dictionaries. The type associated to each dictionary is defined asPokemonincse163_utils.py. To help test functions solved with Pandas, callpd.read_csvto return the dataset as aDataFrame. You can see examples of both functions being used inhw2_test.py -
Create your own testing CSV file by pressing the + icon in the editor and selecting New file. When specifying file names in your own IDE or on Ed, use absolute paths, such as
/home/pokemon_test.csv. When you are submitting to Gradescope, make sure to update these to be relative paths, such aspokemon_test.csv. (This ensures that the Gradescope tests run correctly.) Do not use thepokemon_box.csvfile in your own test cases. The file is too large to come up with the correct answer on your own—it’s not valid to run your code and then paste it as the expected output. -
Write at least one test function for each problem and give it a descriptive name that indicates the function being tested, such as
test_species_count. One test function per problem is fine since both ways of solving the problem should compute the same result. In addition to the providedpokemon_test.csvexample, add one additional test case for each problem (both manual andpandasimplementation) using your own data.
Note
Do not use the pokemon_box.csv file in your own test cases. The file is too large to come up with the correct answer on your own—it’s not valid to run your code and then paste it as the expected output.
species_count¶
Task: Write in hw2_manual.py a function species_count that takes a parsed pokemon dataset and returns the number of unique pokemon species in the dataset as determined by the name attribute without using Pandas.
For the pokemon_test.csv file, species_count(data) should return 3.
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame.
Info
For typing a parameter of type DataFrame, use the type pd.DataFrame (assuming pandas is imported as pd).
Task: Write in hw2_test.py one or more test functions that call species_count with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
max_level¶
Task: Write in hw2_manual.py a function max_level that takes a parsed pokemon dataset and returns a 2-element tuple of the (name, level) of the pokemon with the highest level in the dataset. If there is more than one pokemon with the highest level, return the pokemon that appears first in the file.
For the pokemon_test.csv file, max_level(data) should return the 2-element tuple, ('Lapras', 72).
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame.
Task: Write in hw2_test.py one or more test functions that call max_level with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
filter_range¶
Task: Write in hw2_manual.py a function filter_range that takes a parsed pokemon dataset and two integers: a lower bound (inclusive) and upper bound (exclusive). The function returns a list of the names of pokemon whose level fall within the bounds in the same order that they appear in the dataset.
For the pokemon_test.csv file, filter_range(data, 35, 72) should return ['Arcanine', 'Arcanine', 'Starmie']. Note that Lapras is not included because the upper bound is exclusive of Lapras, which is exactly level 72.
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame. To convert a Pandas Series to a list, use the built-in list function.
For example:
# data is a DataFrame storing following info:
# name,age,species
# Fido,4,dog
# Meowrty,6,cat
# Chester,1,dog
# Phil,1,axolotl
names = data['name'] # Series
list(names) # ['Fido', 'Meowrty', 'Chester', 'Phil']
row = data.loc[1] # Series
list(row) # ['Meowrty', 6, 'cat']
Task: Write in hw2_test.py one or more test functions that call filter_range with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
mean_attack_for_type¶
Task: Write in hw2_manual.py a function mean_attack_for_type that takes a parsed pokemon dataset and a str representing the pokemon type. The function returns the average atk for all the pokemon in the dataset with the given type. If there are no pokemon of the given type, return None.
For the pokemon_test.csv file, mean_attack_for_type(data, 'fire') should return 47.5.
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame.
Task: Write in hw2_test.py one or more test functions that call mean_attack_for_type with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
count_types¶
Task: Write in hw2_manual.py a function count_types that takes a parsed pokemon dataset and returns a dictionary representing for each pokemon type the number of pokemon of that type. The order of the keys in the returned dictionary does not matter.
For the pokemon_test.csv file, count_types(data) should return {'fire': 2, 'water': 2}.
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame. To convert a Pandas Series to a dict, use the built-in dict function. The dictionary keys are determined by the series index.
For example:
# data is a DataFrame storing following info:
# name,age,species
# Fido,4,dog
# Meowrty,6,cat
# Chester,1,dog
# Phil,1,axolotl
names = data['name'] # Series
dict(names) # {0: 'Fido', 1: 'Meowrty', 2: 'Chester', 3: 'Phil'}
row = data.loc[1] # Series
dict(row) # {'name': 'Meowrty', 'age': 6, 'species': 'cat'}
Task: Write in hw2_test.py one or more test functions that call count_types with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
mean_attack_per_type¶
Task: Write in hw2_manual.py a function mean_attack_per_type that takes a parsed pokemon dataset and returns a dictionary representing for each pokemon type the average atk of pokemon of that type. The order of the keys in the returned dictionary does not matter.
For the pokemon_test.csv file, mean_attack_per_type(data) should return {'fire': 47.5, 'water': 140.5}.
Task: Write in hw2_pandas.py the same function using Pandas. Rather than take a list of dictionaries, this takes a Pandas DataFrame.
Task: Write in hw2_test.py one or more test functions that call mean_attack_per_type with some CSV files and compares the output of the program with the expected value using assert_equals. In addition to the pokemon_test.csv file, add one additional test case for both the manual and pandas implementation by creating your own CSV file.
Quality¶
Assessment submissions should pass these checks: flake8, and code quality guidelines. The code quality guidelines are very thorough. For this assessment, the most relevant rules can be found in these sections (new sections bolded):
-
-
Boolean Zen
-
Loop Zen
-
Factoring
-
Unnecessary Cases
-
Avoid Looping with Pandas
-
Reminder
Make sure to provide descriptive comments in docstring format including at the top of every module you write!
Submission¶
Submit your work by uploading the following files to Gradescope:
hw2_manual.pyhw2_pandas.pyhw2_test.pypokemon_test.csv- Any other
.csvfiles that you created and used for testing.
Submit as often as you want until the deadline for the initial submission. Note that we will only grade your most recent submission.
Please make sure you are familiar with the resources and policies outlined in the syllabus and the take-home assessments page.
THA 2 - Pokemon
Initial Submission by Thursday 07/17 at 11:59 pm.
Submit on Gradescope