1 of 20

CSE 163

Section XX

TA 1 & TA 2

Question of the Day: If you could travel anywhere in the world right now, where would you go?

2 of 20

Housekeeping 🏡

  • Programming Practice #6 due today
  • HW4: Networks & Section Check-In #7 due tomorrow (Friday, 7/31 @ 11:59PM)
    • Resub cycle also closes
  • Project Part 2: EDA due Sunday (8/2) at 11:59pm

3 of 20

Game Plan

What We’ll Cover Today

  • Review
    • Geospatial Data
  • Practice Problems
    • geospatial-data-win26.ipynb

4 of 20

Recap

What we’ve learned so far:

  • GeoDataFrames
    • geometry column
      • Points, Polygons, MultiPolygons
    • plt.subplots()

5 of 20

Geospatial Data

GeoDataFrame:

  • Just like a regular dataframe but with a geometry column
    • Will plot a map by default
  • Usually json, shp, geojson files.
  • Can do all the fun pandas filtering you’re used to!

import geopandas as gpd

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

df = gpd.read_file(DATA_FILE)

sa = df[df['CONTINENT'] == 'South America’]

sa.plot(column='POP_EST', legend=True)

6 of 20

Subplots

Subplots

  • Ask matplotlib to directly give you a Figure and Axes obj
  • Specify number of rows and columns
  • Can do cool background effect by plotting on the same axis twice

Figure: the big canvas

Axes: various parts of that big canvas

import matplotlib.pyplot as plt

axs = [[ax1, ax2, ax3], [ax4, ax5, ax6]]

fig, axs = plt.subplots(nrows=2, ncols=3)

7 of 20

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’

7

8 of 20

SettingWithCopy warning

What is it, and how can we work around it?

9 of 20

Let’s look at an example!

This code produces a SettingWithCopy warning!

import pandas as pd

tas = pd.DataFrame([

{‘first’: ‘Arona’, ‘last’: ‘Cho’, ‘pet’: ‘cat’},

{‘first’: ‘Renusree’, ‘last’: ‘Chittella’, ‘pet’: ‘dog’},

{‘first’: ‘Arpan’, ‘last’: ‘Kapoor’, ‘pet’: ‘cat’},

{‘first’: ‘Hannah’, ‘last’: ‘Chiu’, ‘pet’: ‘dog’},

{‘first’: ‘Asmi’, ‘last’: ‘Sathaye’, ‘pet’: ‘dog’},

{‘first’: ‘Mia’, ‘last’: ‘Wang’, ‘pet’: ‘cat’}

])

dog_mask = tas[‘pet’] == ‘dog’

dog_lovers = tas[dog_mask]

dog_lovers[‘full_name’] = dog_lovers[‘first’] + ‘ ‘ + dog_lovers[‘last’]

# uh oh!

10 of 20

What is it?

  • This line of code triggered the warning: dog_lovers[‘full_name’] = dog_lovers[‘first’] + ‘ ‘ + dog_lovers[‘last’]
  • The warning often appears when we chain operations
    • Chaining occurs when you perform multiple operations in sequence, such as selecting and modifying data in one line, which can lead to ambiguity about whether you're working with a view or a copy of the DataFrame.

  • When we chain operations, pandas warns us to be explicit to avoid confusion about modifying the original dataset
  • The first lines of code ( dog_mask = tas[‘pet’] == ‘dog’

dog_lovers = tas[dog_mask]) did not cause the error because they only involve selection, not modification, so there’s no risk of unintended data changes at that point.

11 of 20

What should we do instead?

  • Use .loc[] for selection and modification in one step:
    • Instead of chaining operations, select and modify data in a single line using .loc[] (e.g., df.loc[condition, 'column'] = value).
    • Adding columns directly (e.g., df['new_col'] = 20) is perfectly fine. The SettingWithCopyWarning only arises when you filter or subset your DataFrame first and then try to modify the resulting subset.

  • Use .copy()
    • When called, a new object will be created with a copy of the calling object’s data and indices. Modifications to the data or indices of the copy will not be reflected in the original object
    • This prevents accidental modification of the original DataFrame and leads to clearer, more predictable code.

dog_lovers = tas[dog_mask].copy()

12 of 20

Spatial Join and merge

  • merge combines two tabular datasets (can be DF or GeoDF) on specific columns using exact == matches
    • By default, performs an inner join
    • on=””, right_on=””, left_on=””, how=”inner”
  • sjoin allows us to specify geospatial operations to link two datasets
    • sjoin combines two geospatial datasets on their geometry columns using geometric intersection
    • how=”inner” (default), how=”left”

13 of 20

Merge

  • merge combines two tabular datasets on specific columns using exact == matches
    • By default, performs an inner join
    • on=””, right_on=””, left_on=””

14 of 20

Merge for left vs right

When merging GDF with regular DF, the order matters!

  • gdf.merge(df, …) → keeps GeoDataFrame behavior
  • df.merge(gdf, …) → returns a regular DataFrame
    • geometry column gets dropped

Keep gdf on the left when you want to preserve geometry and map/plot

(important for __init__ in THA 5)

15 of 20

What’s the difference?

  • sjoin combines data based on geometries, while merge combines data on matching attribute values

  • NOTE: when you call .merge(), the outputted object will be the type of what you passed as the left dataframe in your .merge()
    • gdf.merge(df, on=””) outputs a GeoDataFrame
    • df.merge(gdf, on=””) outputs a DataFrame

16 of 20

Practice problems!

Open up geospatial-data-win26.ipynb

17 of 20

Solutions!

18 of 20

Solutions!

19 of 20

Project Part 2: EDA Check In

Now that we’re a little over halfway through the quarter, think about and discuss the following with your group:

  • Briefly describe your project to your group members. What dataset and questions are you working with?
  • What is the current status of your EDA? What preliminary findings are you going to discuss?
  • What was the feedback you got from the TA in your proposal, and how do you plan to incorporate it into your EDA?

After discussing, group members should ask follow-up questions or offer insights!

20 of 20

Section Code: