Task 4: Dataframe Modifications#
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
Dataframe Modification#
Dataframes can be manipulated and modified in many ways! Pandas provides you with many tools/methods to do this. For this Task, we will be exploring two different methods; apply and assign. The documentation gives you an overview of what each method does, but we will learn how to best utilize these methods effectively.
Firstly, let’s get a proper dataframe to work with! Let’s import the Pokemon dataset again from this link, and save it into a dataframe called df
.
df = pd.read_csv('https://github.com/firasm/bits/raw/master/pokemon.csv')
Task 4.1: apply()#
Let’s have a look at the apply()
method. As you’ve read from the documentation, the method can take in any function, and apply any pre-defined function to your dataframe!
This allows you to make some fairly sophisticated additions and changes to your dataframe, and can show you extremely useful information!
4.1.1#
Let’s start off with something simple! The pokemon that you’ve found have been attacked with a virus that cuts their health points! Due to this virus, all pokémon health points (HP_ have been reduced the cube root of their original HP.
Take the cube-root all of the HP values in the df
you made above using the apply()
function.
Your final dataframe should match something this:
Sample Output
# |
Name |
Type 1 |
Type 2 |
Total |
HP |
Attack |
Defense |
Sp. Atk |
Sp. Def |
Speed |
Generation |
Legendary |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
1 |
Bulbasaur |
Grass |
Poison |
318 |
3.55689 |
49 |
49 |
65 |
65 |
45 |
1 |
False |
1 |
2 |
Ivysaur |
Grass |
Poison |
405 |
3.91487 |
62 |
63 |
80 |
80 |
60 |
1 |
False |
2 |
3 |
Venusaur |
Grass |
Poison |
525 |
4.30887 |
82 |
83 |
100 |
100 |
80 |
1 |
False |
3 |
3 |
VenusaurMega Venusaur |
Grass |
Poison |
625 |
4.30887 |
100 |
123 |
122 |
120 |
80 |
1 |
False |
4 |
4 |
Charmander |
Fire |
nan |
309 |
3.39121 |
52 |
43 |
60 |
50 |
65 |
1 |
False |
5 |
5 |
Charmeleon |
Fire |
nan |
405 |
3.87088 |
64 |
58 |
80 |
65 |
80 |
1 |
False |
# Your solution here
As you can see, it is possible to modify just value within a single column, but it requires some manoeuvring!
Nice job!
4.1.2#
After figuring out who attacked them, all 800 of your pokemon are ready to fight back! You use a stardust and candy to give them a temporary increase to their health status, and double their attack power!
Create a new column called
Temporary Health
that returns their health value back to normal. (Hint: You used a cube root above, how would you reverse that?)Create a new column called
Temporary Attack
that doubles their attack power.
Your final dataframe should match something this:
Sample Output
# |
Name |
Type 1 |
Type 2 |
Total |
HP |
Attack |
Defense |
Sp. Atk |
Sp. Def |
Speed |
Generation |
Legendary |
Temporary Health |
Temporary Attack |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
1 |
Bulbasaur |
Grass |
Poison |
318 |
3.55689 |
49 |
49 |
65 |
65 |
45 |
1 |
False |
45 |
98 |
1 |
2 |
Ivysaur |
Grass |
Poison |
405 |
3.91487 |
62 |
63 |
80 |
80 |
60 |
1 |
False |
60 |
124 |
2 |
3 |
Venusaur |
Grass |
Poison |
525 |
4.30887 |
82 |
83 |
100 |
100 |
80 |
1 |
False |
80 |
164 |
3 |
3 |
VenusaurMega Venusaur |
Grass |
Poison |
625 |
4.30887 |
100 |
123 |
122 |
120 |
80 |
1 |
False |
80 |
200 |
4 |
4 |
Charmander |
Fire |
nan |
309 |
3.39121 |
52 |
43 |
60 |
50 |
65 |
1 |
False |
39 |
104 |
5 |
5 |
Charmeleon |
Fire |
nan |
405 |
3.87088 |
64 |
58 |
80 |
65 |
80 |
1 |
False |
58 |
128 |
# Your solution here
As you can see, the apply()
method can have a lot of uses! Nice job figuring out how to use the method effectively in conjunction with your own pre-defined methods.
Task 4.2: Assign#
Let’s have a look at the assign method! It will create new columns and return the original dataframe with the new columns that were just made, this is especially useful when having aggregate columns e.g Task 3.1 makes you create a new column based on their scores.
Before we get started, re-import the pokemon dataset into the same variable df
.
df = pd.read_csv('https://github.com/firasm/bits/raw/master/pokemon.csv')
4.2.1#
Create a new column called Special_Power
that multiplies the values of the Sp. Atk
and Sp. Def
.
Your final dataframe should match something this:
Sample Output
# |
Name |
Type 1 |
Type 2 |
Total |
HP |
Attack |
Defense |
Sp. Atk |
Sp. Def |
Speed |
Generation |
Legendary |
Special_Power |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
1 |
Bulbasaur |
Grass |
Poison |
318 |
45 |
49 |
49 |
65 |
65 |
45 |
1 |
False |
4225 |
1 |
2 |
Ivysaur |
Grass |
Poison |
405 |
60 |
62 |
63 |
80 |
80 |
60 |
1 |
False |
6400 |
2 |
3 |
Venusaur |
Grass |
Poison |
525 |
80 |
82 |
83 |
100 |
100 |
80 |
1 |
False |
10000 |
3 |
3 |
VenusaurMega Venusaur |
Grass |
Poison |
625 |
80 |
100 |
123 |
122 |
120 |
80 |
1 |
False |
14640 |
4 |
4 |
Charmander |
Fire |
nan |
309 |
39 |
52 |
43 |
60 |
50 |
65 |
1 |
False |
3000 |
5 |
5 |
Charmeleon |
Fire |
nan |
405 |
58 |
64 |
58 |
80 |
65 |
80 |
1 |
False |
5200 |
# Your solution here
4.2.2#
Create a new column called Type_Combined
that combines both types of all the pokemon into one large string.
Your final dataframe should match something this:
Sample Output
# |
Name |
Type 1 |
Type 2 |
Total |
HP |
Attack |
Defense |
Sp. Atk |
Sp. Def |
Speed |
Generation |
Legendary |
Special_Power |
Type_Combined |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
1 |
Bulbasaur |
Grass |
Poison |
318 |
45 |
49 |
49 |
65 |
65 |
45 |
1 |
False |
4225 |
GrassPoison |
1 |
2 |
Ivysaur |
Grass |
Poison |
405 |
60 |
62 |
63 |
80 |
80 |
60 |
1 |
False |
6400 |
GrassPoison |
2 |
3 |
Venusaur |
Grass |
Poison |
525 |
80 |
82 |
83 |
100 |
100 |
80 |
1 |
False |
10000 |
GrassPoison |
3 |
3 |
VenusaurMega Venusaur |
Grass |
Poison |
625 |
80 |
100 |
123 |
122 |
120 |
80 |
1 |
False |
14640 |
GrassPoison |
4 |
4 |
Charmander |
Fire |
nan |
309 |
39 |
52 |
43 |
60 |
50 |
65 |
1 |
False |
3000 |
nan |
5 |
5 |
Charmeleon |
Fire |
nan |
405 |
58 |
64 |
58 |
80 |
65 |
80 |
1 |
False |
5200 |
nan |
# Your solution here