1 of 19

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 19

Housekeeping 🏡

​

​

Important Dates and Reminders

  • Section Check-in 4 due tomorrow at 11:59 PM
  • Programming Practice 3 due 10/25 11:59 PM
  • Project Proposal due Tuesday 10/27 11:59 PM
  • Homework 3: Education due 10/29 11:59 PM

​

​

3 of 19

How to find your homework feedback

  1. Navigate to your submission in Gradescope
  2. Click on the ‘Code’ tab on the left and scroll through each file to see the comments left by the TA
  3. On the right hand side, click ‘Quality’ or another rubric category to see the deductions given

​

4 of 19

Game Plan

What We’ll Cover Today

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

�

5 of 19

Recap

What we’ve learned so far:

Last week:

  • Groupby
  • Hierarchical indexing

This week:

  • Data visualization
    • Seaborn
    • Matplotlib

6 of 19

Groupby Demo

7 of 19

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 19

Hierarchical Indexing

9 of 19

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 19

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 19

Data Viz

12 of 19

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 19

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 19

Axes vs. FacetGrid

What’s the difference?

15 of 19

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 19

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 19

Practice Problems!

​

18 of 19

Section code:

​

19 of 19

Solutions

​

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

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