comparison operators - amazon s3 · boolean operators in [12]: import numpy as np country capital...
TRANSCRIPT
![Page 1: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/1.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Comparison Operators
![Page 2: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/2.jpg)
Intermediate Python for Data Science
Numpy Recap
Comparison operators: how Python values relate
In [1]: import numpy as np In [2]: np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79]) In [3]: np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7]) In [4]: bmi = np_weight / np_height ** 2
In [5]: bmi Out[5]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
In [6]: bmi > 23 Out[6]: array([False, False, False, True, False], dtype=bool)
In [7]: bmi[bmi > 23] Out[7]: array([ 24.747]) Code from Intro to Python for Data Science, Chapter 4
![Page 3: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/3.jpg)
Intermediate Python for Data Science
Numeric ComparisonsIn [8]: 2 < 3 Out[8]: True
In [9]: 2 == 3 Out[9]: False
In [10]: 2 <= 3 Out[10]: True
In [11]: 3 <= 3 Out[11]: True
In [12]: x = 2
In [13]: y = 3
In [14]: x < y Out[14]: True
![Page 4: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/4.jpg)
Intermediate Python for Data Science
Other ComparisonsIn [15]: "carl" < "chris" Out[15]: True
In [16]: 3 < "chris" TypeError: unorderable types: int() < str()
In [17]: 3 < 4.1 Out[17]: True
In [18]: bmi Out[18]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
In [19]: bmi > 23 Out[19]: array([False, False, False, True, False], dtype=bool)
![Page 5: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/5.jpg)
Intermediate Python for Data Science
Comparators< strictly less than
<= less than or equal
> strictly greater than
>= greater than or equal
equal
!= not equal
==
![Page 6: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/6.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!
![Page 7: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/7.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Boolean Operators
![Page 8: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/8.jpg)
Intermediate Python for Data Science
Boolean Operators● and
● or
● not
![Page 9: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/9.jpg)
Intermediate Python for Data Science
andIn [1]: True and True Out[1]: True
In [2]: False and True Out[2]: False
In [3]: True and False Out[3]: False
In [4]: False and False Out[4]: False
In [5]: x = 12
In [6]: x > 5 and x < 15 True True
Out[6]: True
![Page 10: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/10.jpg)
Intermediate Python for Data Science
orIn [7]: True or True Out[7]: True
In [8]: False or True Out[8]: True
In [9]: True or False Out[9]: True
In [10]: False or False Out[10]: False
In [11]: y = 5
In [12]: y < 7 or y > 13 True False
Out[12]: True
![Page 11: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/11.jpg)
Intermediate Python for Data Science
notIn [13]: not True Out[13]: False
In [14]: not False Out[14]: True
![Page 12: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/12.jpg)
Intermediate Python for Data Science
NumpyIn [19]: bmi # calculation of bmi left out Out[19]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
In [20]: bmi > 21 Out[20]: array([ True, False, True, True, True], dtype=bool)
In [21]: bmi < 22 Out[22]: array([ True, True, True, False, True], dtype=bool)
In [23]: bmi > 21 and bmi < 22 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
![Page 13: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/13.jpg)
Intermediate Python for Data Science
NumpyIn [19]: bmi # calculation of bmi left out Out[19]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
In [20]: bmi > 21 Out[20]: array([ True, False, True, True, True], dtype=bool)
In [21]: bmi < 22 Out[22]: array([ True, True, True, False, True], dtype=bool)
In [23]: bmi > 21 and bmi < 22 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [24]: np.logical_and(bmi > 21, bmi < 22) Out[24]: array([ True, False, True, False, True], dtype=bool)
logical_and() logical_or() logical_not()
In [25]: bmi[np.logical_and(bmi > 21, bmi < 22)] Out[25]: array([ 21.852, 21.75, 21.441])
![Page 14: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/14.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!
![Page 15: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/15.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
if, elif, else
![Page 16: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/16.jpg)
Intermediate Python for Data Science
Overview● Comparison Operators
● <, >, >=, <=, ==, !=
● Boolean Operators
● and, or, not
● Conditional Statements
● if, else, elif
![Page 17: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/17.jpg)
Intermediate Python for Data Science
if
z = 4 if z % 2 == 0 : print("z is even")
control.py!
True
if condition : expression
"
Output: z is even
![Page 18: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/18.jpg)
Intermediate Python for Data Science
if
z = 4 if z % 2 == 0 : print("z is even")
control.py!
True
if condition : expressionexpression # not part of if
"
Output: z is even
![Page 19: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/19.jpg)
Intermediate Python for Data Science
z = 4 if z % 2 == 0 : print("checking " + str(z)) print("z is even")
control.py!
if if condition : expression
"
Output: checking 4z is even
![Page 20: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/20.jpg)
Intermediate Python for Data Science
z = 5 if z % 2 == 0 : print("checking " + str(z)) print("z is even")
control.py!
if if condition : expression
"
Output:
False
Not executed
![Page 21: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/21.jpg)
Intermediate Python for Data Science
z = 5 if z % 2 == 0 : print("z is even")
control.py!
False
else if condition : expression else : expression
"
Output: z is odd
else : print("z is odd")
![Page 22: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/22.jpg)
Intermediate Python for Data Science
elif
z = 3 if z % 2 == 0 : print("z is divisible by 2") elif z % 3 == 0 : print("z is divisible by 3") else : print("z is neither divisible by 2 nor by 3")
control.py!
Output: z is divisible by 3
False
True
if condition : expression elif condition : expression else : expression
"
![Page 23: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/23.jpg)
Intermediate Python for Data Science
elif
z = 6 if z % 2 == 0 : print("z is divisible by 2") elif z % 3 == 0 : print("z is divisible by 3") else : print("z is neither divisible by 2 nor by 3")
control.py!
Output: z is divisible by 2
True
Never reached
if condition : expression elif condition : expression else : expression
"
![Page 24: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/24.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!
![Page 25: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/25.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Filtering Pandas DataFrame
![Page 26: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/26.jpg)
Intermediate Python for Data Science
bricsIn [1]: import pandas as pd
In [2]: brics = pd.read_csv("path/to/brics.csv", index_col = 0)
In [3]: brics Out[3]: country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
![Page 27: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/27.jpg)
Intermediate Python for Data Science
Goal● Select countries with
area over 8 million km2
● 3 steps
● Select the area column
● Do comparison on area column
● Use result to select countries
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
![Page 28: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/28.jpg)
Intermediate Python for Data Science
Step 1: Get columnIn [4]: brics["area"] Out[4]: BR 8.516 RU 17.100 IN 3.286 CH 9.597 SA 1.221 Name: area, dtype: float64 Need Pandas Series
brics.iloc[:,2]
brics.loc[:,"area"]
Alternatives:
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
![Page 29: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/29.jpg)
Intermediate Python for Data Science
Step 2: CompareIn [4]: brics["area"] Out[4]: BR 8.516 RU 17.100 IN 3.286 CH 9.597 SA 1.221 Name: area, dtype: float64
Out[5]: BR True RU True IN False CH True SA False Name: area, dtype: bool
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
In [5]: brics["area"] > 8
In [6]: is_huge = brics["area"] > 8
![Page 30: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/30.jpg)
Intermediate Python for Data Science
Step 3: Subset DFIn [7]: is_huge Out[7]: BR True RU True IN False CH True SA False Name: area, dtype: bool
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
In [8]: brics[is_huge] Out[8]: country capital area population BR Brazil Brasilia 8.516 200.4 RU Russia Moscow 17.100 143.5 CH China Beijing 9.597 1357.0
![Page 31: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/31.jpg)
Intermediate Python for Data Science
SummaryIn [9]: is_huge = brics["area"]
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98> 8
In [10]: brics[is_huge] Out[10]: country capital area population BR Brazil Brasilia 8.516 200.4 RU Russia Moscow 17.100 143.5 CH China Beijing 9.597 1357.0
In [11]: brics[brics["area"] > 8] Out[11]: country capital area population BR Brazil Brasilia 8.516 200.4 RU Russia Moscow 17.100 143.5 CH China Beijing 9.597 1357.0
![Page 32: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/32.jpg)
Intermediate Python for Data Science
Boolean operatorsIn [12]: import numpy as np
country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
In [13]: np.logical_and(brics["area"] > 8, brics["area"] < 10) Out[13]: BR True RU False IN False CH True SA False Name: area, dtype: bool
In [14]: brics[np.logical_and(brics["area"] > 8, brics["area"] < 10)] Out[14]: country capital area population BR Brazil Brasilia 8.516 200.4 CH China Beijing 9.597 1357.0
![Page 33: Comparison Operators - Amazon S3 · Boolean operators In [12]: import numpy as np country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN](https://reader030.vdocuments.us/reader030/viewer/2022040410/5ed0975378612700be1f06e6/html5/thumbnails/33.jpg)
INTERMEDIATE PYTHON FOR DATA SCIENCE
Let’s practice!