1

I have two datasets that I want to use. The first is a shapefile of King County that has polygons broken up by school districts. It has boundaries saved as polygon features on a geo-dataframe.

The other is a regular data frame with houses but with an engineered series that is the coordinate stored as a geometry point.

I want to take the boundaries of the districts to group the houses by school district.

The districts look like this.

   OID  D#  NAME                              geometry
0   1   1   Seattle           MULTIPOLYGON (((-122.40324 47.66637, -122.4028...
1   2   210 Federal Way       POLYGON ((-122.29057 47.39374, -122.29039 47.3...
2   3   216 Enumclaw          POLYGON ((-121.84898 47.34708, -121.84908 47.3...
3   4   400 Mercer Island     POLYGON ((-122.24475 47.59601, -122.24461 47.5...
4   5   401 Highline          POLYGON ((-122.35853 47.51553, -122.35851 47.5...
5   6   402 Vashon Island     POLYGON ((-122.47330 47.51112, -122.47302 47.5...
6   7   403 Renton            POLYGON ((-122.19290 47.54932, -122.19281 47.5...
7   8   404 Skykomish         POLYGON ((-121.43171 47.78048, -121.41064 47.7...
8   9   405 Bellevue          POLYGON ((-122.21511 47.65306, -122.21511 47.6...
9   10  406 Tukwila 17406     POLYGON ((-122.27170 47.49817, -122.27149 47.4...
10  11  407 Riverview         POLYGON ((-121.95169 47.77663, -121.94866 47.7...
11  12  408 Auburn            POLYGON ((-122.23531 47.35371, -122.23497 47.3...
12  13  409 Tahoma            POLYGON ((-121.89772 47.48571, -121.89505 47.4...
13  14  410 Snoqualmie Valley POLYGON ((-121.68436 47.68667, -121.68177 47.6...
14  15  411 Issaquah          POLYGON ((-122.07115 47.60205, -122.07085 47.6...
15  16  412 Shoreline         POLYGON ((-122.35372 47.77777, -122.35367 47.7...
16  17  414 Lake Washington   POLYGON ((-122.24520 47.73180, -122.24520 47.7...
17  18  415 Kent              POLYGON ((-122.12172 47.46185, -122.12182 47.4...
18  19  417 Northshore        POLYGON ((-122.25070 47.77705, -122.25047 47.7...
19  20  888 Fife              POLYGON ((-122.28191 47.27161, -122.28074 47.2...

The houses have this set up (edited and truncated for clarity)

        id       price            geometry
0   2591820310  365000.0    POINT (-122.16200 47.43880)
1   7974200820  865000.0    POINT (-122.28500 47.67840)
2   7701450110  1038000.0   POINT (-122.12900 47.56460)
3   9522300010  1490000.0   POINT (-122.22800 47.69950)
4   9510861140  711000.0    POINT (-122.08300 47.66470)

I was able to use the names in the geodataframe itself to annotate the plot like so:

left = ['Riverview','Snoqualmie Valley']
center = ['Skykomish','Kent','Auburn','Tahoma','Vashon Island','Northshore','Shoreline','Renton','Highline',
         'Issaquah','Enumclaw','Seattle','Federal Way','Bellevue','Mercer Island','Lake Washington','Tukwila']
right = ['Fife']

kings_county.plot(figsize = (15,15),cmap = 'gist_earth')

for idx, row in kings_county.iterrows():
    if row['NAME'] in left:
        plt.annotate(s=row['NAME'], xy=row['coords'],
                 ha='left', color = 'red')
    elif row['NAME'] in center:
        plt.annotate(s=row['NAME'], xy=row['coords'],
                 ha='center', color = 'red')
    elif row['NAME'] in right:
        plt.annotate(s=row['NAME'], xy=row['coords'],
                 ha='right', color = 'red')
plt.title('School Districts in Kings County, WA', fontdict = {'fontsize': 20})
plt.ylabel('Latitude',fontdict = {'fontsize': 20})
plt.xlabel('Longitude',fontdict = {'fontsize': 20})

But not sure how to combine the information.

I have all of this housing data with individual points, I want to group them based on their district. I know this would involve something like what I've done above with the names but not sure how.

2
  • 1
    Trying to understand what exactly you want to do, but it feels like you want a a spatial join and count all the houses in each polygon. If so, try the accepted answer here
    – Dror Bogin
    Aug 17 '20 at 8:49
  • could you elaborate more on what you mean by "combine"? I believe "combine" means "merging the attribute in districts polygon to the corresponding/overlapping house points"? If so, I'll go with @DrorBogin
    – sutan
    Aug 24 '20 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.