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:¶

  1. Remove all PokƩmon 6th generation and above.

  2. Remove the Legendary column.

  3. Remove all rows that contain ā€œFormeā€, a special form of PokĆ©mon.

  4. 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