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.

Dictionaries (Słowniki) Dodawanie: europe['italy'] = 'rome' Wypisanie: print(europe["italy"]) Słownik: europe = { 'spain': { 'capital':'madrid', 'population':46.77 }, 'france': { 'capital':'paris', 'population':66.03 }, 'germany': { 'capital':'berlin', 'population':80.62 }, 'norway': { 'capital':'oslo', 'population':5.084 } }

Print out the capital of France

print(europe['france']['capital'])

Create sub-dictionary data

data = {'capital':'rome', 'population': 59.83}

Add data to europe under key 'italy'

europe['italy'] = {'capital': 'rome', 'population': 59.83}

Print europe

print(europe)

#PANDAS

Pre-defined lists

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

import pandas as pd

Create dictionary my_dict with three key:value pairs: my_dict

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

Build a DataFrame cars from my_dict: cars

cars = pd.DataFrame(my_dict)

Print cars

print(cars)

Build cars DataFrame

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] cars_dict = { 'country':names, 'drives_right':dr, 'cars_per_cap':cpc } cars = pd.DataFrame(cars_dict) print(cars)

Definition of row_labels

row_labels = ['US', 'AUS', 'JPN', 'IN', 'RU', 'MOR', 'EG']

Specify row labels of cars

cars.index = row_labels

Import the cars.csv data: cars

cars = pd.read_csv("cars.csv")

Fix import by including index_col

cars = pd.read_csv('cars.csv',index_col=0)

Print out country column as Pandas DataFrame

print(cars[['country']])

Print out DataFrame with country and drives_right columns

print(cars[['country', 'drives_right']])

Print out fourth, fifth and sixth observation

print(cars.iloc[3:6])

Print out observations for Australia and Egypt

print(cars.loc[['AUS', 'EG']])

Print out drives_right value of Morocco

print(cars.loc['MOR', 'drives_right'])

Print sub-DataFrame

print(cars.loc[['RU', 'MOR'], ['country', 'drives_right']])

Print out drives_right column as Series

print(cars.loc[:, 'drives_right'])

Print out drives_right column as DataFrame

print(cars.loc[:, [ 'drives_right']])

Print out cars_per_cap and drives_right as DataFrame

print(cars.loc[:, [ 'cars_per_cap', 'drives_right']])

# 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!".