본문 바로가기
Real Estate Data/아파트

아파트 파이썬 데이터 분석 시리즈 1 - 2 [2021년 ~ 2023년 인천시 월별 아파트 거래건수 및 평당 실거래가]

by 동장군님 2024. 2. 14.
728x90
반응형

1. 지난 포스팅에서 가지고온 인천시 아파트 거래 데이터를 가지고 계속 분석을 해보겠다.

 

2. 이번에는 지난 3년간 월별 아파트 거래건수와 평당 실거래가를 확인해보겠다.

 

3. 기존 데이터 전처리를 통해 월별 거래건수와 평당 실거래가를 가지고 왔다.

import seaborn as sns
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle

incheon=pd.DataFrame(data)
incheon['거래금액']=incheon['거래금액'].apply(lambda x:int(x.replace(',','')))
inc=incheon.sort_values(by='거래금액',ascending=False)[['거래금액','법정동','아파트','년','월','층','전용면적']]
inc['평수']=inc['전용면적'].apply(lambda x: round(float(x)*0.3025,2))
inc['월']=inc['월'].apply(lambda x: '0'+x if int(x)<10 else x )
inc['거래연월']=inc['년']+inc['월']


inc_sum=inc.groupby(['거래연월']).sum()
inc_sum['평당 실거래가']=round(inc_sum['거래금액']/inc_sum['평수'],0)
inc_sum['평당 실거래가']=inc_sum['평당 실거래가'].apply(lambda x:int(x))
inc_count=inc.groupby(['거래연월']).count()
fd=pd.concat([inc_count[['거래금액']],inc_sum[['평당 실거래가']]], axis=1)
fd.columns=['거래건수','평당 실거래가']

 

4. 위 데이터를 기준으로 그래프를 그려봤다. 

 

fig, ax1 = plt.subplots(figsize=(20,10))
color = 'tab:green'
#bar plot creation
ax1.set_xlabel('거래연월', fontsize=12)
ax1.set_ylabel('거래건수', fontsize=12)
ax1 = sns.barplot(x=fd.index, y='거래건수', data = fd, palette='summer')
ax1.tick_params(axis='y')
ax1.set_xticklabels(labels=fd.index,rotation=45,fontsize=12)


for i in fd.index:
    ax1.annotate(f"{fd['거래건수'][i]}", 
                   xy=(list(fd.index).index(i), fd['거래건수'][i]+100),
                   va = 'center', ha='center',fontweight='light',fontsize=11,
                   color=color)

#specify we want to share the same x-axis
ax2 = ax1.twinx()
color = 'tab:red'
# #line plot creation
ax2.set_ylabel('평당 실거래가', fontsize=12)
ax2 = sns.lineplot(x=fd.index, y='평당 실거래가', data = fd, sort=False, color=color)
ax2.tick_params(axis='y', color=color)
ax2.set(ylim=(0, 3000))


for i in fd.index:
    ax2.annotate(f"{fd['평당 실거래가'][i]}", 
                   xy=(list(fd.index).index(i), fd['평당 실거래가'][i]+100),
                   va = 'center', ha='center',fontweight='light',fontsize=11,
                   color='tab:red')
    
plt.title('2021년 ~ 2023년 월별 인천 아파트 거래건수 및 평당 거래금액')




ax1.legend(handles = [Rectangle((0, 0), 0, 0, color = "tab:green", alpha = 1, label = "거래건수"),
                                                  Rectangle((0, 0), 0, 0, color = "tab:red", alpha = 1, label = "평당실거래가")],
            loc = "lower left", bbox_to_anchor = (0, 0.9, 1, 0), ncol = 1, fontsize = "small", title_fontsize = "medium")



#show plot
plt.show()

 

5. 평당 거래가는 21년 이후 큰 변화는 없어보이지만 확실히 시장 금리가 높다보니 거래량이 엄청나게 감소함

 

 

 

 

 

728x90
반응형

댓글