Skip to content

Intermediate Python

Run the hidden code cell below to import the data used in this course.

# Import the course packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Import the two datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
brics = pd.read_csv("datasets/brics.csv")

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Opsomming van lessen:

  • dictionary
  • gebruik van pandas

Pre-defined lists - opbouw list

names = ['United States', 'Australia', 'Japan', 'India', 'Russia', 'Morocco', 'Egypt'] dr = [True, False, False, False, True, True, True] cpc = [809, 731, 588, 18, 200, 70, 45]

Import pandas as pd - standard import

import pandas as pd

Create dictionary my_dict with three key:value pairs: my_dict - dictionary koppelen?

my_dict = { 'country':names, 'drives_right':dr, 'cars_per_cap':cpc }

Build a DataFrame cars from my_dict: cars - DF creëren met pd

cars = pd.DataFrame(my_dict)

Print cars

print(cars)

Om een row te selecteren kun je het volgende gebruiken: brics[1:4]

Hiermee selecteer je rij 2 t/m 5, merk op dat rijen bij 0 beginnen!

In het kort

  • square brackets - column access brics[['country, capital']] - row access: brics[1:4]
  • loc (label-based) - row access brics.loc[['x', 'x', 'x']] - column access: brics.loc[:, ['x', 'x']] - row and column access: brics.loc[['x', 'x'], ['x', 'x']]

Met iloc kun je nummers gebruiken

  • iloc voorbeeld: brics.iloc[[1,2], [1,2]]

Print out first 3 observations

print(cars[0:3])

Print out fourth, fifth and sixth observation

print(cars[3:6])

Loops

  • je kunt een oneindige loop stoppen door ctrl + c

Voorbeeld loop:

Initialize offset

offset = -6

Code the while loop

while offset != 0 : print("correcting...") if offset > 0 : offset = offset - 1 else : offset = offset + 1
print(offset)

Code for loop

areas list

areas = [11.25, 18.0, 20.0, 10.75, 9.50]

Change for loop to use enumerate() and update print()

for index, area in enumerate(areas) : print("room " + str(index) + ": " + str(area))

house list of lists

house = [["hallway", 11.25], ["kitchen", 18.0], ["living room", 20.0], ["bedroom", 10.75], ["bathroom", 9.50]]

Build a for loop from scratch

for x in house : print("the " + x[0] + " is " + str(x[1]) + " sqm")

Import numpy as np

import numpy as np

For loop over np_height

for el in np_height : print(str(el) + " inches")

For loop over np_baseball

for el in np.nditer(np_baseball) : print(el)

NumPy is imported, seed is set

Initialize random_walk

random_walk = [0]

Complete the loop

for x in range(100) : # Set step: last element in random_walk step = random_walk[-1]

# Roll the dice dice = np.random.randint(1,7) # Determine next step if dice <= 2: step = step - 1 elif dice <= 5: step = step + 1 else: step = step + np.random.randint(1,7) # append next_step to random_walk random_walk.append(step)

Print random_walk

print(random_walk)

numpy and matplotlib imported, seed set.

initialize and populate all_walks

all_walks = [] for i in range(10) : random_walk = [0] for x in range(100) : step = random_walk[-1] dice = np.random.randint(1,7) if dice <= 2: step = max(0, step - 1) elif dice <= 5: step = step + 1 else: step = step + np.random.randint(1,7) random_walk.append(step) all_walks.append(random_walk)

Convert all_walks to NumPy array: np_aw

np_aw = np.array(all_walks)

Plot np_aw and show

plt.plot(np_aw) plt.show()

Clear the figure

plt.clf()

Transpose np_aw: np_aw_t

np_aw_t = np.transpose(np_aw)

Plot np_aw_t and show

plt.plot(np_aw_t) plt.show()

Een manier om aantallen te tellen met numpy:

  • print(np.count_nonzero(ends >= 60))
Spinner
DataFrameas
df
variable
# Add your code snippets here

Explore Datasets

Use the DataFrames imported in the first cell to explore the data and practice your skills!

  • Create a loop that iterates through the brics DataFrame and prints "The population of {country} is {population} million!".
  • Create a histogram of the life expectancies for countries in Africa in the gapminder DataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins.
  • Simulate 10 rolls of two six-sided dice. If the two dice add up to 7 or 11, print "A win!". If the two dice add up to 2, 3, or 12, print "A loss!". If the two dice add up to any other number, print "Roll again!".