1 of 20

CSE 163

Groupby and Apply�

Suh Young Choi�

🎶 Listening to: Minecraft soundtrack

💬 Before Class: If you were a kitchen appliance, what would you be?

2 of 20

This Time

  • Keyword Arguments
  • Groupby
  • Apply

Last Time

  • Imports
  • Pandas
    • How to read a file
    • Accessing columns
    • Element-wise operators
    • Filtering
    • .loc

2

3 of 20

Keyword Arguments

  • Can specify parameters “by position” or “by name”

3

def div(a, b):

return a / b

# Same behavior

div(1, 2)

div(a=1, b=2)

div(b=2, a=1)

# Different behavior

div(b=1, a=2)

4 of 20

DataFrame

  • One of the basic data types from pandas is a DataFrame
    • It’s essentially a table with column and rows!

4

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

Columns

Index (row)

5 of 20

Groupby Demo

 

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

6 of 20

Groupby Demo

 

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

A Groupby DataFrame

A

C

B

7 of 20

Groupby Demo

B

2

C

8

A

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’)[‘col2’].sum()

col2

.sum()

.sum()

.sum()

A

C

B

8 of 20

Groupby Demo

 

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

col2

A

5

B

2

C

8

result

A Series

A

C

B

9 of 20

Groupby Demo

 

col1

col2

col3

0

A

1

25.1

1

B

2

3.9

2

C

3

0.01

3

A

4

6.2

4

C

5

9.44

A

1

25.1

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

What if we had another column?

B

2

3.9

C

3

0.01

A

4

6.2

C

5

9.44

10 of 20

Groupby Demo

 

col1

col2

col3

0

A

1

25.1

1

B

2

3.9

2

C

3

0.01

3

A

4

6.2

4

C

5

9.44

A

1

25.1

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

col2

What if we had another column?

B

2

3.9

C

3

0.01

A

4

6.2

C

5

9.44

col3

A

C

B

11 of 20

Groupby Demo

 

col1

col2

col3

0

A

1

25.1

1

B

2

3.9

2

C

3

0.01

3

A

4

6.2

4

C

5

9.44

A

1

25.1

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

col2

What if we had another column?

B

2

3.9

C

3

0.01

A

4

6.2

C

5

9.44

col3

A

5

B

2

C

8

result

A

C

B

12 of 20

Apply Demo

12

data['name'].apply(len)

 

name

0

‘word’

1

‘hi’

2

‘UW’

3

‘CSE163’

4

‘!!!’

13 of 20

Apply Demo

13

data['name'].apply(len)

 

name

0

‘word’

1

‘hi’

2

‘UW’

3

‘CSE163’

4

‘!!!’

len( )

14 of 20

Apply Demo

14

data['name'].apply(len)

 

name

0

4

1

‘hi’

2

‘UW’

3

‘CSE163’

4

‘!!!’

len( )

15 of 20

Apply Demo

15

data['name'].apply(len)

 

name

0

4

1

2

2

‘UW’

3

‘CSE163’

4

‘!!!’

len( )

16 of 20

Apply Demo

16

data['name'].apply(len)

 

name

0

4

1

2

2

2

3

‘CSE163’

4

‘!!!’

len( )

17 of 20

Apply Demo

17

data['name'].apply(len)

 

name

0

4

1

2

2

2

3

6

4

‘!!!’

len( )

18 of 20

Apply Demo

  • Note that this was just an operation!
  • In order to access these values, you’d have to save it to itself or create a new column

18

data['name'].apply(len)

 

name

0

4

1

2

2

2

3

6

4

3

len( )

19 of 20

Notes on Applying

  • When you pass in functions to apply, you are calling this function for every element in the series
  • So something like this won’t work:

19

def my_function(a: int, b: str) -> int:

return a + len(b)

data['name'].apply(my_function)

20 of 20

Before Next Time

  • Complete Lesson 8
    • Remember not for points, but do go towards Checkpoint Tokens
  • THA1 + LR1 due tomorrow night!
  • Go to section!

Next Time

  • A couple more Pandas functions
  • Data visualization with a new library!

20