In this homework, you will use pandas to inspect, clean, and summarise a dataset. Work slowly and check your output after every step.
You will:
- load a CSV file with pandas
- inspect rows, columns, and data types
- clean column names
- handle missing values
- calculate basic statistics
- group data by category
- create a new calculated column
- save a cleaned dataset
Use this file:
session7/datasets/Pokemon.csvThis CSV uses an older text encoding. Load it like this:
import pandas as pd
pokemon = pd.read_csv("datasets/Pokemon.csv", encoding="cp1252")Run your script from the session7 folder:
python solutions/exercise-07-homework.pyCreate this file:
session7/solutions/exercise-07-homework.pyComplete all 10 tasks in your Python file.
- Load
Pokemon.csvinto a DataFrame calledpokemon. - Print the first 10 rows and the last 5 rows.
- Print the number of rows and columns.
- Print the column names and data types.
- Rename the columns so they are easier to use in Python:
#->pokemon_idName->nameType 1->type_1Type 2->type_2Total->totalHP->hpAttack->attackDefense->defenseSp. Atk->sp_atkSp. Def->sp_defSpeed->speedStage->stageLegendary->legendary
- Check missing values in every column. Fill missing
type_2values with"None". - Print summary statistics for the numeric columns.
- Find and print:
- the Pokemon with the highest
attack - the Pokemon with the highest
defense - the Pokemon with the highest
speed
- the Pokemon with the highest
- Group by
type_1and print:- the number of Pokemon per type
- the average
totalscore per type, sorted from highest to lowest
- Create a new column called
power_scoreusing this formula:
power_score = attack + defense + speedThen print the top 10 Pokemon by power_score and save the cleaned DataFrame to:
session7/solutions/pokemon_clean.csvAt the bottom of your Python file, add comments answering these questions:
- Which column needed the most obvious cleaning?
- Why is it useful to rename columns before analysis?
- Which
type_1has the highest averagetotalscore? - What is one limitation of ranking Pokemon only by
power_score?
- Use pandas.
- Do not edit the original dataset in
datasets/. - Keep your solution in
solutions/. - Use clear variable names.
- Print enough output to show that each task works.
- Add short comments where you make a cleaning decision.
Try the homework yourself before checking the reference solution.