카테고리 없음

[250426] 심화프로젝트 주말...

jeonieee 2025. 4. 27. 13:52
#  동네별 room_type 비율
room_type_ratio = airbnb.groupby(['neighbourhood_group_cleansed', 'room_type']).size().unstack().fillna(0)
room_type_ratio = room_type_ratio.div(room_type_ratio.sum(axis=1), axis=0)

#  동네별 숙소 수
neighbourhood_counts = airbnb['neighbourhood_group_cleansed'].value_counts()

#  각 지역 면적 (직접 수치 입력)
area_km2 = {
    'Manhattan': 59,
    'Brooklyn': 183,
    'Queens': 283,
    'Bronx': 109,
    'Staten Island': 151
}

#  밀집도 계산
density = neighbourhood_counts / pd.Series(area_km2)

#  정리해서 보기
summary = pd.DataFrame({
    '숙소수': neighbourhood_counts,
    '면적(㎢)': pd.Series(area_km2),
    '숙소 밀집도(개/㎢)': density
}).join(room_type_ratio)

print(summary)
  숙소 수 면적(km^2) 숙소 밀집도 (개/km^2) Entire home/apt Hotel room Private room Shared room
Manhattan 10205 59 172.966102 0.681431 0.034003 0.282411 0.002156
Brooklyn 7455 183 40.737705 0.506372 0.002280 0.486385 0.004963
Queens 3420 283 12.084806 0.411696 0.002339 0.578070 0.007895
Bronx 912 109 8.366972 0.405702 0.000000 0.594298 0.000000
Staten Island 316 151 2.092715 0.496835 0.000000 0.503165 0.000000

 

import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(8,5))
sns.barplot(x=summary.index, y='숙소 밀집도(개/㎢)', data=summary, palette='viridis', width=0.5)
plt.title('동네별 숙소 밀집도 (개/㎢)')
plt.ylabel('숙소 밀집도 (개/㎢)')
plt.xlabel('Neighbourhood Group')
plt.xticks(rotation=45)
plt.grid(axis='y')
plt.show()

room_type_cols = ['Entire home/apt', 'Private room', 'Shared room', 'Hotel room']

summary[room_type_cols].plot(kind='bar', stacked=True, figsize=(10,6), colormap='Set3')
plt.title('동네별 Room Type 비율')
plt.ylabel('비율')
plt.xlabel('Neighbourhood Group')
plt.legend(title='Room Type', bbox_to_anchor=(1.05, 1))
plt.xticks(rotation=45)
plt.grid(axis='y')
plt.show()

plt.figure(figsize=(8,6))
sns.scatterplot(x='면적(㎢)', y='숙소수', data=summary, hue=summary.index, s=100)
for i in range(summary.shape[0]):
    plt.text(summary['면적(㎢)'][i]+2, summary['숙소수'][i], summary.index[i])

plt.title('면적 대비 숙소 수')
plt.xlabel('면적 (㎢)')
plt.ylabel('숙소 수')
plt.grid(True)
plt.show()

 

  • Manhattan은 숙소 밀집도가 169개/㎢로, 전체 지역 중 가장 높음. Private room 비율도 40%로 타 지역 대비 높아, 도심 번화가 특성 반영
  • Staten Island는 숙소 밀집도 2개/㎢로 매우 낮고, Entire home 비율이 85%로 주택 중심의 지역 특성
  • Brooklyn은 다양한 숙소 유형이 공존하며, 지역 내 이질성이 클 가능성 있음
import matplotlib.pyplot as plt
import seaborn as sns

# 한글 깨짐 방지 (필요하면 주석 해제)
# plt.rcParams['font.family'] = 'Malgun Gothic'  

plt.figure(figsize=(8, 6))
data = summary[['숙소 밀집도(개/㎢)']]

sns.heatmap(data, annot=True, fmt=".1f", cmap='YlGnBu', linewidths=0.5, cbar_kws={"label": "숙소 밀집도(개/㎢)"})
plt.title('동네별 숙소 밀집도 Heatmap')
plt.yticks(rotation=0)
plt.show()

 

#price를 정수로 변경
airbnb['price']=airbnb['price'].str.replace('$', '')
airbnb['price']=airbnb['price'].str.replace(',', '').astype(float)

# 1. price 평균 계산
price_mean = airbnb.groupby('neighbourhood_group_cleansed')['price'].mean()

# 2. summary 데이터프레임에 추가
summary['평균 숙소 가격'] = price_mean

# 3. 면적 대비 평균 가격 계산
summary['면적 대비 평균 가격'] = summary['평균 숙소 가격'] / summary['면적(㎢)']

# 4. 확인
print(summary[['평균 숙소 가격', '면적 대비 평균 가격']])

# 5. 시각화 (Bar Plot)
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
summary.sort_values('면적 대비 평균 가격', ascending=False)['면적 대비 평균 가격'].plot(kind='bar', color='skyblue')
plt.title('면적 대비 숙소 평균 가격 (동네별)', fontsize=14, weight='bold')
plt.ylabel('평균 가격 / 면적 (단위: $/㎢)')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

 

  평균 숙소 가격 면적 대비 평균 가격
Manhattan 286.343557 4.853281
Brooklyn 169.646680 0.927031
Queens 126.239474 0.446076
Bronx 121.807018 1.117496
Staten Island 128.344937 0.849966