1 of 12

CSE 163

Groupby & Indexing

Arpan Kapoor�Summer 2026��💭Icebreaker (discuss with neighbors):

What did you do over the long weekend?

Add to our Slido!

slido.com

#2348882

2 of 12

Announcements

  • Programming Practice 2 due Thursday, July 9th at 11:59 PM!
  • Homework 2: Pokemon due Friday, July 10th at 11:59 PM!
  • Grades released for…
    • Week 1 & 2 Lesson Reviews and Section Check-ins
    • Programming Practice 1
  • Make sure to wait for the autograder to finish running for programming practices and homeworks!
  • Final Project Part 1 - Proposal released later today! Due in two weeks.
    • Low stakes assignment meant to get you started thinking about the final project!

slido.com

#2348882

3 of 12

DataFrame

  • One of the basic data types from pandas is a DataFrame!

id

year

month

day

latitude

longitude

name

magnitude

0

nc72666881

2016

7

27

37.672333

-121.619000

California

1.43

1

us20006i0y

2016

7

27

21.514600

94.572100

Burma

4.90

2

nc72666891

2016

7

27

37.576500

-118.859167

California

0.06

Index (row)

Columns

slido.com

#2348882

4 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

slido.com

#2348882

5 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

result = data.groupby(‘col1’)

A

1

A

4

B

2

C

3

C

5

slido.com

#2348882

6 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

result = data.groupby(‘col1’)

A

1

A

4

B

2

C

3

C

5

A Groupby DataFrame

slido.com

#2348882

7 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

A

1

A

4

B

2

C

3

C

5

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

col2

slido.com

#2348882

8 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

A

1

A

4

B

2

C

3

C

5

col2

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

.sum()

.sum()

.sum()

slido.com

#2348882

9 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

col2

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

B

2

C

8

A

5

col2

slido.com

#2348882

10 of 12

Groupby Demo

 

col1 

col2

0

A

1

1

B

2

2

C

3

3

A

4

4

C

5

col2

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

B

2

C

8

A

5

slido.com

#2348882

11 of 12

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

Data�DataFrame

Split

Apply

Combine�Series

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

slido.com

#2348882

12 of 12

Apply

  • We have shown how to filter and group data, but what if you want to transform/modify your data
  • Change numerical data with operators: + , -, /, *, abs(), min(), max(), etc.
  • You can change Strings using built-in methods (e.g. len(), upper(), etc.) or even define your own custom function that works on Strings.

data['name'].str.len()

data['name'].str.upper()

data['name'].apply(len) #Function as parameter

data['name'].apply(my_function) #Custom my_function

slido.com

#2348882