GeoDataFrame으로 살펴보는 파리의 레스토랑
이 장의 첫 번째 코딩 연습에서는 csv 파일에서 파리 레스토랑의 위치를 불러왔어요.
GeoPandas의 공간 기능을 사용하려면 pandas DataFrame을 GeoDataFrame으로 변환해야 해요. 이는 GeoDataFrame() 생성자와 geopandas.points_from_xy() 함수를 사용해 수행할 수 있으며, 이 코드는 이미 준비되어 있어요.
이제 GeoDataFrame이 되었으므로, 지오메트리 플로팅과 같은 모든 공간 기능을 사용할 수 있어요. 이번 연습에서는 레스토랑 데이터셋으로 처음 만들었던 그림을, GeoDataFrame의 plot() 메서드를 사용해 다시 만들어 볼 거예요.
Pandas는 pd, GeoPandas는 geopandas, matplotlib의 pyplot 기능은 plt로 임포트되어 있어요.
이 연습은 강의의 일부입니다
Python으로 지리공간 데이터 다루기
연습 안내
restaurantsGeoDataFrame의 처음 몇 행을 확인하세요.- GeoDataFrame의
plot()메서드로 플롯하세요. 반환값은 matplotlib의 axes 객체이므로, 이를ax라고 부르세요. - 마커 크기는 다시 1로 설정하세요.
contextily를 사용해 베이스맵 레이어를 추가하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Read the restaurants csv file into a DataFrame
df = pd.read_csv("paris_restaurants.csv")
# Convert it to a GeoDataFrame
restaurants = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))
# Inspect the first rows of the restaurants GeoDataFrame
print(restaurants.____)
# Make a plot of the restaurants
ax = restaurants.____
import contextily
contextily.____(____)
plt.show()