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