import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns  # ✅ 이걸 안해서 오류 난 거예요!

# 데이터 불러오기 (파일 경로에 맞게 설정되어야 함)
df = pd.read_csv('AB_NYC_2019.csv')

# 대도시별 방타입별 평균 리뷰 수 계산
avg_reviews_by_city_room = df.groupby(['neighbourhood_group', 'room_type'])['number_of_reviews'].mean().reset_index()
avg_reviews_by_city_room['rounded_reviews'] = avg_reviews_by_city_room['number_of_reviews'].round(1)

# 시각화: 막대그래프 + 수치 표시
plt.figure(figsize=(12, 6))
barplot = sns.barplot(x='neighbourhood_group', y='number_of_reviews', hue='room_type', data=avg_reviews_by_city_room)

# 막대 위에 리뷰 수 수치 표시
for container in barplot.containers:
    barplot.bar_label(container, fmt='%.1f', label_type='edge', fontsize=9)

plt.title('🏠 대도시별 방타입별 평균 리뷰 수 (+ 수치 표시)')
plt.xlabel('대도시 (neighbourhood_group)')
plt.ylabel('평균 리뷰 수')
plt.xticks(rotation=45)
plt.legend(title='방 타입')
plt.tight_layout()
plt.show()

download.png

⇒ 나중에 혹시 필요할까봐

인사이트 도출

맨해튼- 아파트 타입이 가장 많은데, 리뷰 수가 제일 적음 ( 문제 발생)

블루클린 - 개인방 타입이 가장 많은데, 아파트 타입 리뷰가 더 많음.

# 대도시별 리뷰 수 평균, 최댓값, 최솟값 계산
review_stats = df.groupby('neighbourhood_group')['number_of_reviews'].agg(['mean', 'max', 'min']).reset_index()

# 소수점 반올림
review_stats['mean'] = review_stats['mean'].round(1)

# 표 출력
print("📊 대도시별 리뷰 통계 (평균, 최대, 최소)")
print(review_stats)

image.png

import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(14, 7))
sns.boxplot(
    data=df,
    x='neighbourhood_group',
    y='number_of_reviews',
    hue='room_type'
)

plt.title('📦 대도시별 방타입별 리뷰 수 분포 (Boxplot)')
plt.xlabel('대도시 (neighbourhood_group)')
plt.ylabel('리뷰 수 (number_of_reviews)')
plt.ylim(0, df['number_of_reviews'].max() * 1.05)  # 0부터 시작 + 약간의 여유
plt.xticks(rotation=45)
plt.legend(title='방 타입')
plt.tight_layout()
plt.show()

download.png

1. 전체 구조 설명