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?
Housekeeping 🏡
Important Dates and Reminders
Reminders
Game Plan
What We’ll Cover Today
�
Recap
What we’ve learned so far:
Last week:
This week:
Groupby Demo
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
Hierarchical Indexing
Hierarchical Indexing
# 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']
More on Slice
slice has arguments similar to range:
# 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']
Data Viz
Refresher: Libraries!
Libraries allow for greater functionality beyond the Python defaults.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Data Visualization
Using Seaborn and Matplotlib – very easy out-of-the-box
Seaborn
Matplotlib
# 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
Axes vs. FacetGrid
What’s the difference?
Axes
FacetGrid
Practice Problems!
Solutions
—---------------------------------------------------------------------------------
—---------------------------------------------------------------------------------
Project Proposal
More Reminders:
Come to office hours (in-person AND virtual) with any questions!
Section code: