02 03 Logic, Control Flow and Filtering - HannaAA17/Data-Scientist-With-Python-datacamp GitHub Wiki

Comparison Operators

Numeric comparisons(between the same type): < , == , <= , != , ... "carl" < "chiris"

# Comparison of booleans
print(True == False)

# Comparison of integers
print( -5*15 != 75)

# Comparison of strings
print("pyscript" == "PyScript")

# Compare a boolean with an integer
print(True == 1)

<script.py> output:
    False
    True
    False
    True

Compare arrays

# Create arrays
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50])
your_house = np.array([14.0, 24.0, 14.25, 9.0])

# my_house greater than or equal to 18
print(my_house >= 18)

# my_house less than your_house
print(my_house < your_house)

<script.py> output:
    [ True  True False False]
    [False  True  True False]

Boolean Operators

and, or, not

# Define variables
my_kitchen = 18.0
your_kitchen = 14.0

# my_kitchen bigger than 10 and smaller than 18?
print(my_kitchen > 10 and my_kitchen < 18)

# my_kitchen smaller than 14 or bigger than 17?
print(my_kitchen < 14 or my_kitchen > 17)

# Double my_kitchen smaller than triple your_kitchen?
print(my_kitchen * 2 < your_kitchen * 3)

Boolean operators with Numpy
To use these operators with Numpy, you will need np.logical_and(), np.logical_or() and np.logical_not().

# Create arrays
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50])
your_house = np.array([14.0, 24.0, 14.25, 9.0])

# my_house greater than 18.5 or smaller than 10
print(np.logical_or(my_house > 18.5, my_house < 10))

# Both my_house and your_house smaller than 11
print(np.logical_and(my_house < 11, your_house < 11))

<script.py> output:
    [False  True False  True]
    [False False False  True]

Conditional Statement: if,elif,else

# Define variables
room = "bed"
area = 14.0

# if-elif-else construct for room
if room == "kit" :
    print("looking around in the kitchen.")
elif room == "bed":
    print("looking around in the bedroom.")
else :
    print("looking around elsewhere.")

# if-elif-else construct for area
if area > 15 :
    print("big place!")
elif area > 10 :
    print("medium size, nice!")
else :
    print("pretty small.")

<script.py> output:
    looking around in the bedroom.
    medium size, nice!

Filtering pandas DataFrames

  • get column
  • Compare
  • Subset
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Extract drives_right column as Series: dr
dr = cars["drives_right"]

# Use dr to subset cars: sel
sel= cars [dr == True]

# Convert code to a one-liner
# sel = cars[cars['drives_right'] == True]

# Print sel
print(sel)

<script.py> output:
         cars_per_cap        country  drives_right
    US            809  United States          True
    RU            200         Russia          True
    MOR            70        Morocco          True
    EG             45          Egypt          True

pandas is built on Numpy, it's okay to use Numpy boolean operators

# Import numpy, you'll need this
import numpy as np

# Create medium: observations with cars_per_cap between 100 and 500
cpc = cars['cars_per_cap']
medium = cars[np.logical_and(cpc > 100, cpc < 500)]

# Print medium
print(medium)

<script.py> output:
        cars_per_cap country  drives_right
    RU           200  Russia          True
⚠️ **GitHub.com Fallback** ⚠️