1 of 25

CSE 163

Section AX

TA1 & TA2

QOTD (sample): If you were a data structure, which one would you be and why?

2 of 25

Housekeeping 🏡

Important Dates and Reminders

  • NO CLASS JULY 3
  • Programming Practice 1 is due TODAY @ 11:59PM
    • #2 is due next Thursday (already released)
  • Section Check-In 2 is due tomorrow (Friday) @ 11:59PM
  • HW 1: Processing is due Friday (July 03) @ 11:59 PM

All submissions are done through Gradescope

3 of 25

Recap

What we’ve Learned so far:

  • Week 1
    • Basic Python Syntax
      • Control Flow
    • Built-in Data Structures
  • Week 2
    • File Processing
    • Pandas Fundamentals

4 of 25

Game Plan

What We’ll Cover Today

  • Data structures review
  • Dictionary practice problem
  • Dataframes and pandas review
  • Practice problems
  • Groupby (if time)�

5 of 25

List

  • General purpose multi value storage
    • Multi-type
  • Mutable
    • append()
    • extend()
    • remove(), pop()
  • Indexable

Use Cases:

  • Need to store multiple values
  • Unknown number of values

new_list = list()

new_list = []

new_list.append("a") # ["a"]

new_list.append(2) # ["a", 2]

new_list[0] # "a"

new_list.extend(["b"])

# ["a", 2, "b"]

new_list.remove("b")

# ["a", 2]

new_list.pop(0) # returns “a”

# new_list = [2]

6 of 25

Tuple

  • General purpose multi value storage
    • Multi-type
  • Immutable
    • Memory benefits
  • Indexable
  • Can be defined arbitrarily with parentheses or commas
  • Can hold more than two values

Use Cases:

  • Fixed size multi-value situations
  • Optimization

tup = ("hello", "world")

tup[0] # "hello"

# takes in params a and b

def pack(a, b):

# returns a tuple of (a, b)

return a, b

7 of 25

Sets

  • Same-type unordered storage
    • Can only store immutable values
    • Does not store duplicates
  • Lightning fast membership queries
  • Mutable
    • add()
    • remove()
    • update()
    • | → union, & → intersection,

- → difference (in THA2!)

  • Note: Cannot be initialized with {}

Use Cases:

  • Need fast membership queries
  • Need to store only unique values

my_set = set()

my_set.add(10) # {10}

my_set.add(30) # {10, 30}

my_set.add(30) # {10, 30}

my_set.add([]) # Error!

if 10 in my_set: # Membership Query

print("Found 10!")

8 of 25

Dictionaries

  • Stores unordered Key-Value pairs
    • Keys must be immutable (no duplicates)
    • Values can be mutable and duplicated
  • Lots of tooling
    • .keys()
    • .values()
    • .items()
  • Use key-checking pattern

Use Cases:

  • Storing corresponding mappings
    • Student Name -> ID Number
    • Region -> Area Code
  • Very powerful.

d = dict()

d = {} # same symbol as sets!

d['Triangle'] = 3

d['Square'] = 4

d['Square'] # 4

things = ["cat", "hat", "bat", "cat"]

my_dict = dict()

for i in things: # also really fast!

if i not in my_dict:

my_dict[i] = 0

my_dict[i] += 1

my_dict

# {"cat": 2, "hat": 1, "bat": 1}

9 of 25

File Processing

  • “with open” syntax
  • Read with .readlines() function
  • Two approaches:
    • Line based
    • Token based

file_path = "home/doggies.txt"

with open(file_path) as f:

lines = f.readlines()

for l in lines:

print(l)

file_path = "home/doggies.txt"

with open(file_path) as f:

lines = f.readlines()

for l in lines:

words = l.split()

for w in words:

print(w)

10 of 25

10

Practice Problem

11 of 25

Solution

12 of 25

DataFrames Review

13 of 25

Data

Goal: Represent real-world phenomena in a tabular format

CSV

  • Ubiquitous representation of data
  • Comma separated values.
  • First row is columns
    • Every other row is a “record” or instance of data

# cats.csv

import pandas as pd

data = pd.read_csv("cats.csv")

print(data)

===============================

name color age outdoor

0 fluffy ginger 2 True

1 sparky grey 8 False

2 storm black 1 True

===============================

# cats.csv in a list of dicts

cats = [

{"name": "fluffy", "age": 2},

{"name": "sparky", "age": 8},

{"name": "storm", "age": 1}

14 of 25

Processing:

List of dicts

  • Loop through the list of dicts
    • Every iteration will set your iteration variable to a new row
  • Extract variables with dictionary indexing
  • Notice- Row-by-row processing

# cats.csv in a list of dicts

cats = [

{"name": "fluffy", "age": 2},

{"name": "sparky", "age": 8},

{"name": "storm", "age": 1}

]

total_age = 0

for c in cats:

# c stores a cat dictionary

# ex: {"name": __, "age": __}

age = c['age']

total_age += age

total_age # 11

15 of 25

DataFrames

  • Tabular Data
    • Similar to list of dictionaries, but more powerful
    • Columns are Series
    • Single rows are Series
  • Able to get entire columns
    • data[‘column_name’]
    • data[ [‘col1’, ‘col2’] ]
  • Aggregate data
    • data[‘column_name’].sum()
      • .mean(), .max(), .unique()...
  • Series-based operations
    • data[‘column’] + …

import pandas as pd

data = pd.read_csv("cats.csv")

print(data)

===============================

name color age outdoor

0 fluffy ginger 2 True

1 sparky grey 8 False

2 storm black 1 True

===============================

data['age'] # Series([2, 8, 1])

data['age'].sum() # 11

data['age'] * 4 # Series([8, 32, 4])

16 of 25

DataFrame Filtering

  • Masks
    • Boolean Series from a mask
      • data[‘age’] > 3
    • Filter by using mask in dataframe
      • data[data[‘age’] > 3]
    • Combine masks with &, |, ~
      • data[mask1 & mask2]

data['age'] > 1

# Series([True, True, False])

data[data['age'] > 1]

===============================

name color age outdoor

0 fluffy ginger 2 True

1 sparky grey 8 False

===============================

data['outdoor'] == True

# Series([True, False, True])

data[data['outdoor'] == True]

===============================

name color age outdoor

0 fluffy ginger 2 True

2 storm black 1 True

===============================

17 of 25

DataFrame Filtering

cont.

===============================

name color age outdoor

0 fluffy ginger 2 True

1 sparky grey 8 False

2 storm black 1 True

===============================

old_age = data['age'] > 1

# Series([True, True, False])

outdoors = data['outdoor'] == True

# Series([True, False, True])

old_age & outdoors

# Series([True, False, False])

data[old_age & outdoors]

===============================

name color age outdoor

0 fluffy ginger 2 True

===============================

18 of 25

DataFrame Indexing with .loc

  • Use a row_indexer and column_indexer to specify where to extract data.
  • Data.loc[row_indexer, col_indexer]
  • Can also get you single values
    • data.loc[0, ‘age’]
  • Can use slices too!
    • Data.loc[0:2] gets rows 0, 1, and 2
      • Use “:” for “all”
    • Can use lists
      • data.loc[0, [‘name’, ‘age’]

df.loc[0] # same as df.loc[0, :]

# Series(["fluffy", "ginger", 2, True])

df.loc[0:1] # same as df.loc[0:1, :]

# DataFrame

===============================

name color age outdoor

0 fluffy ginger 2 True

1 sparky grey 8 False

===============================

df.loc[0:1, "color"]

# Series(["ginger", "grey"])

19 of 25

19

Practice problems!

20 of 25

Solutions

DF manipulation:

Warm-up

21 of 25

Groupby Demo

22 of 25

Group By

result = data.groupby('col1')['col2'].sum()

22

col1

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

col2

C

3

5

col2

B

2

col2

A

1

4

A

5

B

2

C

8

A

5

B

2

C

8

Data�DataFrame

Split

Apply

Combine�Series

23 of 25

Groupby problems solutions

  1. # first, groupby the city of origin, then access the flavor score column and find the index of the max value

grouped_cities = gsb.groupby("CityOfOrigin")["FlavorScore"].idxmax()

# then, use the grouping from above as our row indexer in .loc (since we used idxmax()) and specify the other columns we want to display

gsb_grouped = gsb.loc[grouped_cities, ["Name", "CityOfOrigin", "FlavorScore"]]

  1. # groupby both columns (order matters), then call .size() to show the count

city_year_count = gsb.groupby(["CityOfOrigin", "StartedBaking"]).size()

24 of 25

24

Section Code:

<section code>

25 of 25

Logistics & Reminders:

  • HW 2: Pokemon released tomorrow, due Friday (July 10) @ 11:59 PM
  • Section Check-In 2 due tomorrow (Friday, July 03) @ 11:59PM

Remember you can come into office hours or post on Ed if you have any questions. Good luck and thanks for coming to section :)