import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport plotly.express as pxfrom plotly.subplots import make_subplotsimport plotly.io as piopio.renderers.default ='notebook_connected'
Loading Data using Pandas:
pd.read_csv()
pd.read_excel()
pd.read_json()
pd.read_table()
Inside the parenthesis you put the exact name/location of the data.
Code Examples
Loading Data from a File on My computer
DF = pd.read_csv('mapdataall.csv')
Loading Data from a File on Joanna’s Website - this code is usually given to you
You can technically leave out the by= command, but this sometimes helps me to understand the command. If you wanted to send in a list of columns, it can sort more than one column at a time. It will sort in the order the columns are given.
Code Examples
Sorting by height descending
DF.sort_values(by='height',ascending=False)
name
height
mass
hair_color
skin_color
eye_color
birth_year
sex
gender
homeworld
species
films
vehicles
starships
55
Yarael Poof
264.0
NaN
none
white
yellow
NaN
male
masculine
Quermia
Quermian
The Phantom Menace
NaN
NaN
78
Tarfful
234.0
136.0
brown
brown
blue
NaN
male
masculine
Kashyyyk
Wookiee
Revenge of the Sith
NaN
NaN
70
Lama Su
229.0
88.0
none
grey
black
NaN
male
masculine
Kamino
Kaminoan
Attack of the Clones
NaN
NaN
12
Chewbacca
228.0
112.0
brown
unknown
blue
200.0
male
masculine
Kashyyyk
Wookiee
A New Hope, The Empire Strikes Back, Return of...
AT-ST
Millennium Falcon, Imperial shuttle
35
Roos Tarpals
224.0
82.0
none
grey
orange
NaN
male
masculine
Naboo
Gungan
The Phantom Menace
NaN
NaN
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
82
Finn
NaN
NaN
black
dark
dark
NaN
male
masculine
NaN
Human
The Force Awakens
NaN
NaN
83
Rey
NaN
NaN
brown
light
hazel
NaN
female
feminine
NaN
Human
The Force Awakens
NaN
NaN
84
Poe Dameron
NaN
NaN
brown
light
brown
NaN
male
masculine
NaN
Human
The Force Awakens
NaN
X-wing
85
BB8
NaN
NaN
none
none
black
NaN
none
masculine
NaN
Droid
The Force Awakens
NaN
NaN
86
Captain Phasma
NaN
NaN
none
none
unknown
NaN
female
feminine
NaN
Human
The Force Awakens
NaN
NaN
87 rows × 14 columns
Boolean Masks
Basic Operators
Operator
Definition
<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
==
exactly equal to
!=
not equal to
Advanced Operators
Operator
Definition
and
check if two things are both true
or
check if one of two things is true
in
checks if something is in another thing
not
checks if something is false
Operators that Combine Masks
Operator
Definition
&
checks that two masks are both true (AND)
|
checks if one of the two masks is true (OR)
when masking more than one condition, separate the conditions with parenthesis:
mask = (condition 1) & (condition 2) - both must be true
mask = (condition 1) | (condition 2) - at least one must be true
DF[mask]
Code Examples
Mask for a value not equal
mask = DF['homeworld'] !='Tatooine'DF[mask]
name
height
mass
hair_color
skin_color
eye_color
birth_year
sex
gender
homeworld
species
films
vehicles
starships
2
R2-D2
96.0
32.0
NaN
white, blue
red
33.0
none
masculine
Naboo
Droid
A New Hope, The Empire Strikes Back, Return of...
NaN
NaN
4
Leia Organa
150.0
49.0
brown
light
brown
19.0
female
feminine
Alderaan
Human
A New Hope, The Empire Strikes Back, Return of...
Imperial Speeder Bike
NaN
9
Obi-Wan Kenobi
182.0
77.0
auburn, white
fair
blue-gray
57.0
male
masculine
Stewjon
Human
A New Hope, The Empire Strikes Back, Return of...
Tribubble bongo
Jedi starfighter, Trade Federation cruiser, Na...
11
Wilhuff Tarkin
180.0
NaN
auburn, grey
fair
blue
64.0
male
masculine
Eriadu
Human
A New Hope, Revenge of the Sith
NaN
NaN
12
Chewbacca
228.0
112.0
brown
unknown
blue
200.0
male
masculine
Kashyyyk
Wookiee
A New Hope, The Empire Strikes Back, Return of...
AT-ST
Millennium Falcon, Imperial shuttle
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
82
Finn
NaN
NaN
black
dark
dark
NaN
male
masculine
NaN
Human
The Force Awakens
NaN
NaN
83
Rey
NaN
NaN
brown
light
hazel
NaN
female
feminine
NaN
Human
The Force Awakens
NaN
NaN
84
Poe Dameron
NaN
NaN
brown
light
brown
NaN
male
masculine
NaN
Human
The Force Awakens
NaN
X-wing
85
BB8
NaN
NaN
none
none
black
NaN
none
masculine
NaN
Droid
The Force Awakens
NaN
NaN
86
Captain Phasma
NaN
NaN
none
none
unknown
NaN
female
feminine
NaN
Human
The Force Awakens
NaN
NaN
77 rows × 14 columns
Mask for a value equal
mask = DF['homeworld'] =='Tatooine'DF[mask]
name
height
mass
hair_color
skin_color
eye_color
birth_year
sex
gender
homeworld
species
films
vehicles
starships
0
Luke Skywalker
172.0
77.0
blond
fair
blue
19.0
male
masculine
Tatooine
Human
A New Hope, The Empire Strikes Back, Return of...
Snowspeeder, Imperial Speeder Bike
X-wing, Imperial shuttle
1
C-3PO
167.0
75.0
NaN
gold
yellow
112.0
none
masculine
Tatooine
Droid
A New Hope, The Empire Strikes Back, Return of...
NaN
NaN
3
Darth Vader
202.0
136.0
none
white
yellow
41.9
male
masculine
Tatooine
Human
A New Hope, The Empire Strikes Back, Return of...
NaN
TIE Advanced x1
5
Owen Lars
178.0
120.0
brown, grey
light
blue
52.0
male
masculine
Tatooine
Human
A New Hope, Attack of the Clones, Revenge of t...
NaN
NaN
6
Beru Whitesun Lars
165.0
75.0
brown
light
blue
47.0
female
feminine
Tatooine
Human
A New Hope, Attack of the Clones, Revenge of t...
NaN
NaN
7
R5-D4
97.0
32.0
NaN
white, red
red
NaN
none
masculine
Tatooine
Droid
A New Hope
NaN
NaN
8
Biggs Darklighter
183.0
84.0
black
light
brown
24.0
male
masculine
Tatooine
Human
A New Hope
NaN
X-wing
10
Anakin Skywalker
188.0
84.0
blond
fair
blue
41.9
male
masculine
Tatooine
Human
The Phantom Menace, Attack of the Clones, Reve...
Zephyr-G swoop bike, XJ-6 airspeeder
Naboo fighter, Trade Federation cruiser, Jedi ...
41
Shmi Skywalker
163.0
NaN
black
fair
brown
72.0
female
feminine
Tatooine
Human
The Phantom Menace, Attack of the Clones
NaN
NaN
60
Cliegg Lars
183.0
NaN
brown
fair
blue
82.0
male
masculine
Tatooine
Human
Attack of the Clones
NaN
NaN
Mask a data frame and save the information in a new variable
mask = DF['height'] >100DF_tall = DF[mask].copy()
Masking more than one thing - OR
# A character can only have one homeworld, so we need OR (|) here, not AND (&)mask = (DF['homeworld'] =='Tatooine') | (DF['homeworld'] =='Naboo') | (DF['homeworld'] =='Alderaan')DF_homeworld = DF[mask].copy()DF_homeworld
name
height
mass
hair_color
skin_color
eye_color
birth_year
sex
gender
homeworld
species
films
vehicles
starships
0
Luke Skywalker
172.0
77.0
blond
fair
blue
19.0
male
masculine
Tatooine
Human
A New Hope, The Empire Strikes Back, Return of...
Snowspeeder, Imperial Speeder Bike
X-wing, Imperial shuttle
1
C-3PO
167.0
75.0
NaN
gold
yellow
112.0
none
masculine
Tatooine
Droid
A New Hope, The Empire Strikes Back, Return of...
NaN
NaN
2
R2-D2
96.0
32.0
NaN
white, blue
red
33.0
none
masculine
Naboo
Droid
A New Hope, The Empire Strikes Back, Return of...
NaN
NaN
3
Darth Vader
202.0
136.0
none
white
yellow
41.9
male
masculine
Tatooine
Human
A New Hope, The Empire Strikes Back, Return of...
NaN
TIE Advanced x1
4
Leia Organa
150.0
49.0
brown
light
brown
19.0
female
feminine
Alderaan
Human
A New Hope, The Empire Strikes Back, Return of...
Imperial Speeder Bike
NaN
5
Owen Lars
178.0
120.0
brown, grey
light
blue
52.0
male
masculine
Tatooine
Human
A New Hope, Attack of the Clones, Revenge of t...
NaN
NaN
6
Beru Whitesun Lars
165.0
75.0
brown
light
blue
47.0
female
feminine
Tatooine
Human
A New Hope, Attack of the Clones, Revenge of t...
NaN
NaN
7
R5-D4
97.0
32.0
NaN
white, red
red
NaN
none
masculine
Tatooine
Droid
A New Hope
NaN
NaN
8
Biggs Darklighter
183.0
84.0
black
light
brown
24.0
male
masculine
Tatooine
Human
A New Hope
NaN
X-wing
10
Anakin Skywalker
188.0
84.0
blond
fair
blue
41.9
male
masculine
Tatooine
Human
The Phantom Menace, Attack of the Clones, Reve...
Zephyr-G swoop bike, XJ-6 airspeeder
Naboo fighter, Trade Federation cruiser, Jedi ...
19
Palpatine
170.0
75.0
grey
pale
yellow
82.0
male
masculine
Naboo
Human
The Empire Strikes Back, Return of the Jedi, T...
NaN
NaN
33
Padmé Amidala
185.0
45.0
brown
light
brown
46.0
female
feminine
Naboo
Human
The Phantom Menace, Attack of the Clones, Reve...
NaN
Naboo fighter, H-type Nubian yacht, Naboo star...
34
Jar Jar Binks
196.0
66.0
none
orange
orange
52.0
male
masculine
Naboo
Gungan
The Phantom Menace, Attack of the Clones
NaN
NaN
35
Roos Tarpals
224.0
82.0
none
grey
orange
NaN
male
masculine
Naboo
Gungan
The Phantom Menace
NaN
NaN
36
Rugor Nass
206.0
NaN
none
green
orange
NaN
male
masculine
Naboo
Gungan
The Phantom Menace
NaN
NaN
37
Ric Olié
183.0
NaN
brown
fair
blue
NaN
male
masculine
Naboo
Human
The Phantom Menace
NaN
Naboo Royal Starship
40
Quarsh Panaka
183.0
NaN
black
dark
brown
62.0
male
masculine
Naboo
Human
The Phantom Menace
NaN
NaN
41
Shmi Skywalker
163.0
NaN
black
fair
brown
72.0
female
feminine
Tatooine
Human
The Phantom Menace, Attack of the Clones
NaN
NaN
58
Gregar Typho
185.0
85.0
black
dark
brown
NaN
NaN
NaN
Naboo
NaN
Attack of the Clones
NaN
Naboo fighter
59
Cordé
157.0
NaN
brown
light
brown
NaN
NaN
NaN
Naboo
NaN
Attack of the Clones
NaN
NaN
60
Cliegg Lars
183.0
NaN
brown
fair
blue
82.0
male
masculine
Tatooine
Human
Attack of the Clones
NaN
NaN
64
Dormé
165.0
NaN
brown
light
brown
NaN
female
feminine
Naboo
Human
Attack of the Clones
NaN
NaN
66
Bail Prestor Organa
191.0
NaN
black
tan
brown
67.0
male
masculine
Alderaan
Human
Attack of the Clones, Revenge of the Sith
NaN
NaN
79
Raymus Antilles
188.0
79.0
brown
light
brown
NaN
male
masculine
Alderaan
Human
A New Hope, Revenge of the Sith
NaN
NaN
Adding a Column to a Data Frame
To add a column you just enter a new column name into DF[] followed by a calculation
DF['new_column_name'] = enter your calculation here
Merge using how=left - results will have ids = 1,2,3
pd.merge(DF_fake1, DF_fake2, on='id',how='left')
id
data1_x
data1_y
0
1
x1
y1
1
2
x2
y2
2
3
x3
NaN
Merge using how=right - results will have ids = 1,2,4
pd.merge(DF_fake1, DF_fake2, on='id',how='right')
id
data1_x
data1_y
0
1
x1
y1
1
2
x2
y2
2
4
NaN
y4
Merge using how=outer - results will have ids = 1,2,3,4
pd.merge(DF_fake1, DF_fake2, on='id',how='outer')
id
data1_x
data1_y
0
1
x1
y1
1
2
x2
y2
2
3
x3
NaN
3
4
NaN
y4
Merge using how=inner - results will have ids = 1,2
pd.merge(DF_fake1, DF_fake2, on='id',how='inner')
id
data1_x
data1_y
0
1
x1
y1
1
2
x2
y2
Using .apply and lambda x: to fix your data
DF['column name'].apply(lambda x: operation on x)
The .apply() command lets you apply a function to every row along your data frame.
lambda is a special function you first write the word lambda and choose a variable followed by a colon.
In the example below we are going to create a column in our data frame that counts the total number of films that a Starwars character has been in
Code Examples
Use apply and lambda to break up string data
DF['number_films'] = DF['films'].apply(lambda x: len(x.split(',')))''' Why does this work???Here we are looking at each piece of data in the films column.The data looks like this: "A New Hope, The Empire Strikes Back, Return of the Jedi, Revenge of the Sith"When we do x.split(',') this returns a list: ["A New Hope", "The Empire Strikes Back", "Return of the Jedi", "Revenge of the Sith"]We find the length of this list to count up the movies!'''DF[['name','number_films']]
name
number_films
0
Luke Skywalker
5
1
C-3PO
6
2
R2-D2
7
3
Darth Vader
4
4
Leia Organa
5
...
...
...
82
Finn
1
83
Rey
1
84
Poe Dameron
1
85
BB8
1
86
Captain Phasma
1
87 rows × 2 columns
Use apply and lambda to convert weight from kg to lbs