1 of 10

CSE 163

Dissolve and Join�

Suh Young Choi�

2 of 10

Announcements

Checkpoint 4 due on Monday

THA 5: Mapping due on Thursday

New Resubmission cycle!

  • THA 4 can be resubmitted

2

3 of 10

This Time

  • Dissolve (groupby for Geopandas)
  • Joining datasets

Last Time

  • Geospatial Data
  • Using Geopandas

3

4 of 10

Geopandas

  • Exactly like pandas, but with some extra features for geo data
  • Types
    • GeoDataFrame
    • GeoSeries

4

import geopandas as gpd

df = gpd.read_file('data_file.shp')

df.plot(column='some_col', legend=True)

plt.savefig('plot.png')

5 of 10

Matplotlib

  • Before, we were using a lot of matplotlib stuff blindly
  • Default plots gets plotted on a global figure (think canvas)
    • Multiple plots would overwrite each other
  • You have to do extra work to get more information on the same set of axes.

5

fig, ax = plt.subplots(1)

<plot1>(ax=ax)

<plot2>(ax=ax)

fig.savefig('plot.png')

6 of 10

Dissolve

  • Exactly the same as a groupby for the the regular columns
    • For the geometry columns, overlays all of the geometries
  • Options for aggfunc
    • ‘first’
    • ‘last’
    • ‘min’
    • ‘max’

6

7 of 10

Join

7

tas.merge(grading, left_on='ta_id',

right_on='grader_id')

ta_name

ta_id

Ryan

1

James

2

Nicole

3

grader_id

student_name

2

Flora

3

Paul

1

Wen

3

Andrew

ta_name

ta_id

grader_id

student_name

Ryan

1

1

Wen

James

2

2

Flora

Nicole

3

3

Paul

Nicole

3

3

Andrew

8 of 10

Types of Join

There are interesting questions of what happens if there are rows that don’t “line up”. Different type of joins differ in how to handle this case.

Types of Joins

left.merge(right, left_on=’lcol’, right_on=’rcol’,

how=’type’)

  • Inner (default): Both values must be present
  • Left: If a value from left has no match, add NaNs
  • Right: If a value from right has no match, add NaNs
  • Outer: If a value from either table has no match, add NaNs

8

9 of 10

Hurricane Florence

9

10 of 10

Before Next Time

  • Complete Lesson 18
    • Remember not for points, but do go towards Weekly Tokens
  • Complete Checkpoint 4!
  • Keep working on THA 5!
  • See you next time :)

Next Time

  • numpy
  • Images

10