1 of 30

CSE 163

Section AX

TA 1 & TA 2

Question of the Day: What food describes your mood?

2 of 30

QoD:

What food describes your current mood?

3 of 30

Housekeeping 🏡

Important Dates and Reminders

  • Take-Home Assessment 1: Processing is due tonight (7/10) @ 11:59 PM.
  • Checkpoint 1: Pandas released tomorrow, due Monday (7/14) @ 11:59 PM.
  • Take-Home Assessment 2: Pokemon released tomorrow, due Thursday (7/17) @ 11:59PM

  • Resub Cycle 1 (HW0) closes Tuesday (7/15) @ 11:59 PM.

4 of 30

Game Plan

What We’ll Cover Today

  • Quick Review
  • Groupby!
  • [problems of your choice] (2 & 6 & …)

5 of 30

Groupby Demo

6 of 30

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

7 of 30

 

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

8 of 30

 

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

9 of 30

 

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’)

10 of 30

 

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’)

11 of 30

 

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’)

12 of 30

 

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’)

13 of 30

 

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

14 of 30

 

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

15 of 30

 

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

16 of 30

 

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

17 of 30

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

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

Recap!

18 of 30

 

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!

19 of 30

 

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

20 of 30

 

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

21 of 30

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

22 of 30

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

23 of 30

DataFrames Review

24 of 30

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}

25 of 30

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

26 of 30

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])

27 of 30

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

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

28 of 30

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

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

29 of 30

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"])

30 of 30

30

Ed Lessons