GGR 274 Week 12 Review (Weeks 1-6)#

Week 1: Intro to Python#

  • Remember when different types of variables were new?

  • And notebooks that could mix code cells and Markdown cells were unfamiliar?

  • When variables and their values were simple?

  • A world where True and False are values just like 3 and “home”.

  • And values have a type.

# Name 3 different types represented below
# Name 2 built in functions
number = 3

isthree = number == 4

print(f"It is {isthree} that number is equal to 4.")

Week 2: Strings, lists, conditionals and Loops#

  • A whirlwind tour of control flow in Python

  • We learned to create lists out of strings

  • To loop over lists

  • To change what code is executed based on some condition.

speech = "Ohhh, I've only just begun to pull the thread on this sweater! Friends! You would think, in a game, where there are only two possible correct choices, that one would stumble into the right answer every so often, wouldn't you? In fact, the probability of never guessing right in the full game is a statistical wonder! And yet, here we are!"

# How many exclamation marks are in that speech?
count = 0
for letter in speech:
    if letter == "!":
        count = count + 1

print(f"There are {count} !'s in that speech.  Brennan sounds incensed.")
# But wait it is easier than that: 

print(f"There are {speech.count("!")} !'s in that speech.")
# How many unique words?
# First let's remove some punctuation marks
speech = speech.replace("!", " ")
speech = speech.replace(",", " ")
speech = speech.replace("?", " ")

# and set it to lower case
speech = speech.lower()

# now split it into a list of words
words = speech.split()

print(words)
unique_words = []

for w in words:
    if w not in unique_words:
        unique_words.append(w)

print(f"The ratio or unique words ({len(unique_words)}) to total words ({len(words)}) is {len(unique_words)/ len(words)}")

Week 3: Files, dictionaries, central tendency measures#

  • CSV files are a convenient way to store lots of data

# Open a file for reading
survey_file = open("ggr274_survey.csv")

# read the file into a list of lines
lines = survey_file.readlines()

print(lines[1])
# First extract the second column which represents year in program into a list:

years = []
for line in lines[1:]:
    data = line.split(',')
    if data[1]:
        years.append(int(data[1]))

print(years)

We talked about mean, median, and mode. Which of these measures would be good ones to use with this data?

What do each of these measures tell us about the data?

import statistics
print(f"The mean is {sum(years) / len(years)}")
print(f"The median is {statistics.median(years)}")
print(f"The mode is {statistics.mode(years)}")

Week 4: Introduction to Pandas#

  • Dataframes

  • Series

  • Selecting columsn

  • Selecting rows using a Boolean Series

import pandas as pd

df = pd.read_csv("ggr274_survey.csv")

df.describe()
# Select two columns from the data frame
df[["id", "cups_per_week"]]
# Create a boolean series
drinks = df["cups_per_week"] > 4

df[drinks].head()

Week 5: Data transformations#

  • create a new column

  • rename columns

  • replace missing values

  • categorical vs quatitative data

# let's compute a weekly commute time
df["commute_time_per_week"] = df["commute_time"] * 5

df.head()
# One way to give a column a new name is to create a dictionary that maps
# the old name (key) to the new name (value)

headings = {"pets_num" : "Number of Pets", 
           "pets_type": "Type of Pets" }

df = df.rename(columns = headings)

df.head()

A suggestion for leaving out code in your presentation#

import pandas as pd
import matplotlib.pyplot as plt

class_data = pd.read_csv("ggr274_survey.csv")


counts = (
    class_data.groupby(["favourite_season", "drink_type"])
      .size()
      .unstack()
)
counts.plot(kind="bar")