1 of 21

CSE 163

Section XX

TA 1 & TA 2

Question of the Day: Would you rather fight 1 bowser sized duck or 10 duck sized bowsers?

2 of 21

Housekeeping 🏡

Important Dates and Reminders

  • Homework 2: Pokemon and Section Check In 3 are due July 10 (this Friday) @ 11:59PM
  • Programming Practice 2 is due today (Thursday) @ 11:59PM
    • Grades released around a week after
  • Resubmission Cycle 1 opens tomorrow (Friday)
    • Look for Ed post with instructions
    • You can re-submit ONE homework per cycle (but it can be any homework)

3 of 21

Reminders

  • After uploading your submission to Gradescope, wait for the autograder to run
    • Check that the tests are passing!
  • All section check-in questions are worth points!
    • Graded on both completion and accuracy- just submitting the section code will NOT get you full points!
  • Most office hours are hybrid
    • There are no “in-person only” OH

4 of 21

Game Plan

What We’ll Cover Today

  • Review
    • Groupby
    • Indexing
    • Data visualization
  • Practice Problems
    • groupby-hierarchical-indexing-win26.ipynb

5 of 21

Recap

What we’ve learned so far:

Last week:

  • Groupby
  • Hierarchical indexing

This week:

  • Data visualization
    • Seaborn
    • Matplotlib

6 of 21

Groupby Demo

7 of 21

Group By

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

7

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

8 of 21

Hierarchical Indexing

9 of 21

Hierarchical Indexing

  • DataFrames can have a hierarchical (MultiIndex) index.
    • Each combination of index level values uniquely identifies a row.
  • You can access rows using .loc[].
    • To select specific index levels, pass a tuple as the row indexer (the first argument to .loc[]), with one element per index level.
    • Use slice(None) inside the tuple (instead of : a colon) to select all values at a given level.

# Create MultiIndex

df = pd.read_csv('cats.csv')

cats = df.set_index(['id','age']).sort_index()

# Finding weights of 5 year old cats

weights = cats.loc[(slice(None), 5), 'weight']

10 of 21

More on Slice

slice has arguments similar to range:

  • start (if None, take everything from the beginning)
    • Default: None
  • end (if None, take everything up to the end, exclusive)
    • Default: None
  • step
    • defaults to 1

# Create MultiIndex

df = pd.read_csv('cats.csv')

cats = df.set_index(['id','age']).sort_index()

# What does this do?

weights = cats.loc[(slice(None), slice(None, 10)), 'weight']

11 of 21

Data Viz

12 of 21

Refresher: Libraries!

Libraries allow for greater functionality beyond the Python defaults.

  • Contains objects and functions that vanilla Python doesn’t!
  • Always remember your import statements

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

13 of 21

Data Visualization

Using Seaborn and Matplotlib – very easy out-of-the-box

Seaborn

  • Specify x, y, and data
    • More arguments for specializations (hue, shape)
  • .relplot, .catplot…

Matplotlib

  • Seaborn is built on mpl
  • Use plt to access figure customizations
    • .xlabel, .ylabel, .title

# Simple line plot

df = pd.read_csv('cats.csv')

sns.relplot(x='age', y='weight', data=df)

# Customizing with mpl

plt.xlabel('Age (cm)')

plt.ylabel('Weight (g)')

plt.title('Avg Cat Weight with Age')

import matplotlib.pyplot as plt

import seaborn as sns

14 of 21

Axes vs. FacetGrid

What’s the difference?

15 of 21

Axes

  • An Axes is an object that represents one plotting area
  • Use them when you want one figure (e.g. one plot where multiple figures are overlaid)

16 of 21

FacetGrid

  • A FacetGrid is a seaborn-specific object that creates multiple Axes
    • relplot()
    • catplot()
  • Axes = one whiteboard
  • FacetGrid = a classroom wall of whiteboards, one per group
    • You still draw on each whiteboard (Axes), but the FacetGrid decides how many there are and how they’re arranged.

17 of 21

Practice Problems!

18 of 21

Solutions

—---------------------------------------------------------------------------------

—---------------------------------------------------------------------------------

19 of 21

Project Proposal

Let’s start thinking about the final project!

  • What topics and/or data settings are you interested in researching?
  • What kind of data do you have experience with or have interacted with?
  • Start skimming through dataset websites like data.gov, Kaggle, US Census, NOAA
  • Any questions?

20 of 21

More Reminders:

  • Homework 3: Education released tomorrow (Friday), due next Friday July 17 @ 11:59PM
  • Programming Practice #3 due next Thursday (July 16) @ 11:59PM (already released)

Come to office hours (in-person AND virtual) with any questions!

21 of 21

Section code: