데이터 분석 59일
태블로 작업
방법: 공식 ZIP Code 경계 데이터와 공간 조인 (GeoPandas)
- ZIP Code 경계 데이터: NYC Open Data: MODZCTA
- geopandas 설치 필요 (pip install geopandas)
코드:
import geopandas as gpd
from shapely.geometry import Point
# 1. 사용자 데이터를 GeoDataFrame으로 변환
gdf = gpd.GeoDataFrame(
df,
geometry=[Point(lon, lat) for lon, lat in zip(df['lon'], df['lat'])]
)
#2. 공식 ZIP Code 경계 데이터 로드
zip_gdf = gpd.read_file('https://data.cityofnewyork.us/resource/pri4-ifjk.geojson')
zip_gdf = zip_gdf[['modzcta', 'geometry']].rename(columns={'modzcta': 'zipcode'})
# 3. 공간 조인 (좌표가 ZIP Code 영역 내에 있는지 확인)
result = gpd.sjoin(gdf, zip_gdf, how='left', predicate='within')
# 4. 중복 제거 (동일 좌표에 여러 ZIP Code가 있을 경우 첫 번째 선택)
result = result.drop_duplicates(subset=['neighbourhood_cleansed', 'geometry'])
# 결과 확인
print(result[['neighbourhood_cleansed', 'zipcode']].head())