CSE 163
Section AX
TA 1 & TA 2
Question of the Day: What food describes your mood?
QoD:
What food describes your current mood?
Housekeeping 🏡
Important Dates and Reminders
Game Plan
What We’ll Cover Today
Groupby Demo
Group By
result = data.groupby('col1')['col2'].sum()
6
| 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
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
data
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
C | 3 |
A | 1 |
A | 4 |
C | 5 |
B | 2 |
result = data.groupby(‘col1’)
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)[‘col2’]
A |
C |
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)[‘col2’].sum()
A |
C |
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 5 |
B | 2 |
result = data.groupby(‘col1’)[‘col2’].sum()
C | 8 |
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 5 |
B | 2 |
result = data.groupby(‘col1’)[‘col2’].sum()
C | 8 |
data
result
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
result = data.groupby(‘col1’)[‘col2’].sum()
Recap!
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)[‘col2’].sum()
A |
C |
Recap!
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)[‘col2’].sum()
A |
C |
Recap!
A | 5 |
B | 2 |
C | 8 |
| col1 | col2 |
0 | A | 1 |
1 | B | 2 |
2 | C | 3 |
3 | A | 4 |
4 | C | 5 |
A | 1 |
B | 2 |
C | 3 |
A | 4 |
C | 5 |
result = data.groupby(‘col1’)[‘col2’].sum()
A |
C |
Recap!
A | 5 |
B | 2 |
C | 8 |
A | 5 |
B | 2 |
C | 8 |
What is the value of result?
result = data.groupby(‘col2’)[‘col1’].max()
Your Turn!
| col1 | col2 |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 3 | 3 |
3 | 4 | 2 |
4 | 5 | 2 |
data
What is the value of result?
result = data.groupby(‘col2’)[‘col1’].max()
Your Turn!
| col1 | col2 |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 3 | 3 |
3 | 4 | 2 |
4 | 5 | 2 |
data
2 | 5 |
3 | 3 |
DataFrames Review
Data
Goal: Represent real-world phenomena in a tabular format
CSV
# 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}
Processing:
List of dicts
# 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
DataFrames
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])
DataFrame Filtering
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
===============================
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])
out_age & outdoors
# Series([True, False, False])
data[old_age & outdoors]
===============================
name color age outdoor
0 fluffy ginger 2 True
===============================
DataFrame Indexing with .loc
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"])
30
Ed Lessons