Task 3: Method Chaining#

Remember to run this cell below to import all the necessary packages needed.

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…).

Load Dataset#

We will start with the original data from the pokémon dataset.

pokemon = pd.read_csv('https://github.com/firasm/bits/raw/master/pokemon.csv')

3.1. Create a Method Chain on the cleaning/wrangling/processing steps.#

You should write “one method chain” that does all these commands:

(Hint: You’ve done these modifications before on Lab 5: Task 1.5)

  1. Drop the ‘Generation’, ‘Sp. Atk’, ‘Sp. Def’, ‘Total’, and the ‘#’ columns

  2. Drop any NaN values in HP, Attack, Defense, Speed

  3. Reset the index to get a new index without missing values

  4. A new column was added called index; remove it.

  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

Load Dataset#

We will start with the original data from the pokémon dataset.

pokemon = pd.read_csv('https://github.com/firasm/bits/raw/master/pokemon.csv')

3.2. Create a second Method Chain to do the tasks below:#

You should write “one method chain” that does all these:

  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