Task 4: Method Chaining
Contents
Task 4: Method Chaining¶
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
Method Chaining¶
Method chaining allows you to apply multiple processing steps to your dataframe in a fewer lines of code so it is more readable. You should avoid having too many methods in your chain, as the more you have in a single chain, the harder it is to debug or troubleshoot. I would target about 5 methods in a chain, though this is a flexible suggestion and you should do what makes your analysis the most readable and group your chains based on their purpose (e.g., loading/cleaning, processing, etcā¦).
Note: See Milestone 3 for a more thorough description of method chaining.
Load Dataset from Task 2¶
Since we are working in a new Jupyter Notebook, the first task is to import the CSV dataset (data/task2.csv) we saved in Task 2.
# Your Solution here
4.1. Create a Method Chain on the cleaning/wrangling/processing steps from sections Task 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5.¶
You should write āone method chainā that does all these commands:
2.5.1. Drop the āGenerationā, āSp. Atkā, āSp. Defā, āTotalā, and the ā#ā columns
2.5.2. Drop any NaN values in HP, Attack, Defense, Speed
2.5.3. Reset the index to get a new index without missing values
2.5.4. A new column was added called
index
; remove it.2.5.5. Calculate a new column called āWeighted Scoreā that computes an aggregate score comprising:
20% āHPā
40% āAttackā
30% āDefenseā
10% āSpeedā
# Your Solution here
4.2. Create a second Method Chain to do the tasks below:¶
Remove all PokƩmon 6th generation and above.
Remove the Legendary column.
Remove all rows that contain āFormeā, a special form of PokĆ©mon.
Remove all rows that contain āMegaā, another weird special form of PokĆ©mon.
Hint: You will need to use the .loc in combination with the anonymous function lambda.
# Your Solution here