<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>房地產 &#8211; FinLab</title>
	<atom:link href="https://www.finlab.tw/tag/%e6%88%bf%e5%9c%b0%e7%94%a2/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Sun, 26 Jul 2020 14:04:50 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://www.finlab.tw/wp-content/uploads/2020/07/favicon.png</url>
	<title>房地產 &#8211; FinLab</title>
	<link>https://www.finlab.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">179699571</site>	<item>
		<title>用程式分析房地產可行嗎？房地產爬蟲教學在這裡！</title>
		<link>https://www.finlab.tw/real-estate-analysis1/</link>
					<comments>https://www.finlab.tw/real-estate-analysis1/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:54 +0000</pubDate>
				<category><![CDATA[實價登入]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[房地產]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=968</guid>

					<description><![CDATA[賺了一輩子的錢，還不如買對一戶房！
非常誇張，但又滿有道理的！假如一定要買房子，要買在哪裡呢？隨著屋齡不同，房價應該要如何變化呢？這些問題，可能只有房地產專家為你解惑，但一般人要買房，除了相信專家以外，還能怎麼辦呢？]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter size-large"><img width="1024" height="577" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-1-7-1024x577.png" alt="thumbnail 1 7" class="wp-image-970" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-7-1024x577.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-7-300x169.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-7-768x433.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-7-1536x865.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-7.png 1997w" sizes="(max-width: 1024px) 100vw, 1024px" title="用程式分析房地產可行嗎？房地產爬蟲教學在這裡！ 1"></figure></div>



<p>賺了一輩子的錢，還不如買對一戶房！<br>非常誇張，但又滿有道理的<br>假如一定要買房子，要買在哪裡呢？<br>如何找到抗跌的地段？<br>隨著屋齡不同，房價應該要如何變化呢？</p>



<p>這些問題，可能只有房地產專家一一為你解惑，<br>但一般人要買房，除了相信專家以外，還能怎麼辦呢？</p>



<p>利用程式來研究，自己當專家<br>現在台灣有實價登入的法規，這些資訊都是公開透明的<br>（雖然實價不等於真的成交價，但應該還是有正相關）</p>



<p>要爬取這些資訊，只要很短的程式碼：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">import requests
import os
import zipfile
import time

def real_estate_crawler(year, season):
  if year > 1000:
    year -= 1911

  # download real estate zip content
  res = requests.get("https://plvr.land.moi.gov.tw//DownloadSeason?season="+str(year)+"S"+str(season)+"&amp;type=zip&amp;fileName=lvr_landcsv.zip")

  # save content to file
  fname = str(year)+str(season)+'.zip'
  open(fname, 'wb').write(res.content)

  # make additional folder for files to extract
  folder = 'real_estate' + str(year) + str(season)
  if not os.path.isdir(folder):
    os.mkdir(folder)

  # extract files to the folder
  with zipfile.ZipFile(fname, 'r') as zip_ref:
      zip_ref.extractall(folder)

  time.sleep(10)</code></pre>



<p>然後我們再寫個 for 迴圈，就可以將近 8 年的房價買賣資訊都爬下來</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">real_estate_crawler(101, 3)
real_estate_crawler(101, 4)

for year in range(102, 108):
  for season in range(1,5):
    print(year, season)
    real_estate_crawler(year, season)

real_estate_crawler(108, 1)
real_estate_crawler(108, 2)</code></pre>



<p>這邊就先簡單的放上爬蟲，也有<a href="https://colab.research.google.com/drive/1uDVzXC9e9hXKxoGPK5mryj-B3NSRxNVl" rel="noreferrer noopener" target="_blank">線上colab範例直接運行</a></p>



<p>之後我們會來研究，<br><a href="https://www.finlab.tw/real-estate-analasys-histograms/">究竟那邊的房價衰退的比較快？</a><br>哪裡是比較抗跌的區域？<br>哪裡的房子最便宜又最抗跌？</p>



<p>假如你對這系列有興趣的話，<br>可以到<a href="https://www.facebook.com/finlab.python/" rel="noreferrer noopener" target="_blank">粉絲團</a>的這篇文章按個讚<br>這樣我才知道這是大家想看的<br>才會繼續往下寫喔！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/real-estate-analysis1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">968</post-id>	</item>
		<item>
		<title>用程式分析房地產可行嗎？房價分析看這裡！</title>
		<link>https://www.finlab.tw/real-estate-analasys-histograms/</link>
					<comments>https://www.finlab.tw/real-estate-analasys-histograms/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:54 +0000</pubDate>
				<category><![CDATA[實價登入]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[房地產]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=972</guid>

					<description><![CDATA[大家都在說房價市場恢復景氣，然而真的是這樣子嗎？你有沒有覺得，怎麼每個人說的話都不一樣？數據雖然都是實價登錄，但有些人就是看到漲，有些人就是看跌？究竟誰說的對，還是必須要親自研究一下數據，才會知道！]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="989" height="487" src="http://34.96.136.135/wp-content/uploads/2020/07/fb11-1.png" alt="fb11 1" class="wp-image-974" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/fb11-1.png 989w, https://www.finlab.tw/wp-content/uploads/2020/07/fb11-1-300x148.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/fb11-1-768x378.png 768w" sizes="(max-width: 989px) 100vw, 989px" title="用程式分析房地產可行嗎？房價分析看這裡！ 2"></figure></div>



<p>大家都在說房價市場恢復景氣，然而真的是這樣子嗎？<br>你有沒有覺得，怎麼每個人說的話都不一樣？<br>數據雖然都是實價登錄，但有些人就是看到漲，有些人就是看跌？<br>究竟誰說的對，還是必須要親自研究一下數據，才會知道！</p>



<h1 id="數據是拿來「調整」的？！">數據是拿來「調整」的？！</h1>



<p>身為一個「曾經」產出學術文章寫手，就會知道這些數據是有很多「可以操作空間」（這樣講對嗎？XD），不要相信任何人幫你統計的數據，因為任何人的數據都有可能想達成某種目的，例如某人要增加流量和公信力，就可以危言聳聽一點，用數據製作房價都在下跌的結論，某房仲想要刺激房地產，就會製作止跌回升的訊號！</p>



<h1 id="難道數據造假？！">難道數據造假？！</h1>



<p>不論上漲下跌，相信這些數據都是對的，都是從政府的實價登錄而得到，但分析方式不同，就會產生不同的結果！</p>



<p>所以數據是用來「感覺」的，而不是單看少數「專家」的結論，只能多做一點實驗，盡量讓實驗客觀公正。</p>



<p>這篇文章總共分成三個部分：</p>



<ol><li>獲取實價登錄</li><li>房價歷史走勢圖</li><li>房價分佈圖</li></ol>



<p>所以接下來我們就用 Python 來跟大家一起分析實價登錄的資料吧！</p>



<h1 id="取得實價登錄資料">取得實價登錄資料</h1>



<p>首先跟上次一樣，<a href="https://www.finlab.tw/real-estate-analysis1/">爬取了實價登入所有歷史數據</a>，這次我們爬取csv檔：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">import requests
import os
import zipfile
import time

def real_estate_crawler(year, season):
    if year > 1000:
        year -= 1911

    # download real estate zip file
    res = requests.get("https://plvr.land.moi.gov.tw//DownloadSeason?season="+str(year)+"S"+str(season)+"&amp;type=zip&amp;fileName=lvr_landcsv.zip")

    # save content to file
    fname = str(year)+str(season)+'.zip'
    open(fname, 'wb').write(res.content)

    # make additional folder for files to extract
    folder = 'real_estate' + str(year) + str(season)
    if not os.path.isdir(folder):
        os.mkdir(folder)

    # extract files to the folder
    with zipfile.ZipFile(fname, 'r') as zip_ref:
        zip_ref.extractall(folder)

    time.sleep(10)</code></pre>



<p>有了上述這個 function 我們可以將實價登錄資訊全部爬取下來：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">real_estate_crawler(101, 3)
real_estate_crawler(101, 4)

for year in range(102, 108):
    for season in range(1,5):
        print(year, season)
        real_estate_crawler(year, season)

real_estate_crawler(108, 1)
real_estate_crawler(108, 2)</code></pre>



<p>下載完後，會看到每一年每一季的實價登錄資料夾，裡面有很多檔案，主要可以分成以下三種：</p>



<ul><li>x_lvr_land_a：房屋買賣交易</li><li>x_lvr_land_b：新成屋交易</li><li>x_lvr_land_c：租房交易</li></ul>



<p>其中 x 是一個英文字母，代表每個縣市，也就是你身份證字號的開頭，<br>例如台北，就是「a」，新北市就是「f」，以此類推。</p>



<h1 id="讀取資料">讀取資料</h1>



<p>接下來我們以台北市為例子，看看能不能找到台北市便宜的好房子，<br>首先我們將歷年資料都讀進來：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">import os
import pandas as pd

# 歷年資料夾
dirs = [d for d in os.listdir() if d[:4] == 'real']

dfs = []

for d in dirs:
    print(d)
    df = pd.read_csv(os.path.join(d,'a_lvr_land_a.csv'), index_col=False)
    df['Q'] = d[-1]
    dfs.append(df.iloc[1:])
    
df = pd.concat(dfs, sort=True)</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="626" src="http://34.96.136.135/wp-content/uploads/2020/07/dfhead-1-1024x626.png" alt="dfhead 1" class="wp-image-976" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dfhead-1-1024x626.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/dfhead-1-300x183.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/dfhead-1-768x469.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/dfhead-1-1536x939.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/dfhead-1.png 1986w" sizes="(max-width: 1024px) 100vw, 1024px" title="用程式分析房地產可行嗎？房價分析看這裡！ 3"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/dfhead.png"></a>然而這些資訊還必須再經過處理，才會讓我們待會的資料分析更好下手！</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers"># 新增交易年份
df['year'] = df['交易年月日'].str[:-4].astype(int) + 1911

# 不同名稱同項目資料合併
df['單價元平方公尺'].fillna(df['單價元/平方公尺'], inplace=True)
df.drop(columns='單價元/平方公尺')

# 平方公尺換成坪
df['單價元平方公尺'] = df['單價元平方公尺'].astype(float)
df['單價元坪'] = df['單價元平方公尺'] * 3.30579

# 建物型態
df['建物型態2'] = df['建物型態'].str.split('(').str[0]

# 刪除有備註之交易（多為親友交易、價格不正常之交易）
df = df[df['備註'].isnull()]

# 將index改成年月日
df.index = pd.to_datetime((df['交易年月日'].str[:-4].astype(int) + 1911).astype(str) + df['交易年月日'].str[-4:] ,errors='coerce')</code></pre>



<p>接下來我們可以來看一下這些資料有哪些欄位：</p>



<pre class="wp-block-code"><code class="">df.columns</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="166" src="http://34.96.136.135/wp-content/uploads/2020/07/dfcolumn-1024x166.png" alt="dfcolumn" class="wp-image-977" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dfcolumn-1024x166.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/dfcolumn-300x49.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/dfcolumn-768x124.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/dfcolumn-1536x248.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/dfcolumn-2048x331.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="用程式分析房地產可行嗎？房價分析看這裡！ 4"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/dfcolumn.png"></a>上圖我們比較在意的是：</p>



<ul><li>單價元坪：每坪房價是多少</li><li>物件型態：<code>住宅大樓</code>,&nbsp;<code>倉庫</code>,&nbsp;<code>公寓</code>,&nbsp;<code>套房</code>…等</li><li>鄉鎮市區：<code>中山區</code>,&nbsp;<code>中正區</code>,&nbsp;<code>信義區</code>,&nbsp;<code>內湖區</code>…等</li><li>每年房價的變化</li></ul>



<p>接下來我們就來將上述這些數據，變化成一些圖表，方便我們以視覺化的方式來理解資料。</p>



<h1 id="圖表分析">圖表分析</h1>



<p>老實說，每個建商給的房價走勢圖好像都不太一樣，我不知道他們是怎麼處理這些數據，有時候走勢都好棒棒的感覺，至少finlab的處理的方式，是完全透明，攤在陽光下讓大家知道，我覺得「公佈程式」就是一種比較公正、透明、公開的方式<br>讓大家檢驗這樣的計算是否公正，假如哪裡可以再改進，也可以跟我說！</p>



<h2 id="每年房價走勢圖">每年房價走勢圖</h2>



<p>下圖我們就來計算歷年房價的走勢圖，我們希望每一區可以分開畫，方便我們瞭解<code>地區</code>、<code>時間</code>這兩個因子對於房價的差異：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">
prices = {}
for district in set(df['鄉鎮市區']):
    cond = (
        (df['主要用途'] == '住家用')
        &amp; (df['鄉鎮市區'] == district)
        &amp; (df['單價元坪'] &lt; df["單價元坪"].quantile(0.95))
        &amp; (df['單價元坪'] > df["單價元坪"].quantile(0.05))
        )
    
    groups = df[cond]['year']
    
    prices[district] = df[cond]['單價元坪'].astype(float).groupby(groups).mean().loc[2012:]
    
price_history = pd.DataFrame(prices)
price_history.plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="918" height="540" src="http://34.96.136.135/wp-content/uploads/2020/07/dts.png" alt="dts" class="wp-image-978" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dts.png 918w, https://www.finlab.tw/wp-content/uploads/2020/07/dts-300x176.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/dts-768x452.png 768w" sizes="(max-width: 918px) 100vw, 918px" title="用程式分析房地產可行嗎？房價分析看這裡！ 5"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/dts.png"></a>上圖中我們可以看出來，雖然房價會隨著時間波動，但地段的優勢還是非常的明顯，例如大安區還是很可怕的，一坪最高可以到90萬元（平均後的數字），然而以2019年來說，可以發現：</p>



<p>高價位地段 -&gt; 稍微下跌<br>低價位地段 -&gt; 稍微上漲</p>



<p>畢竟台北的大眾交通工具也算是還OK，住哪裡應該都不至於太不方便，所以在幾乎沒有炒作空間的狀況下，不同地區的差異性慢慢降低，感覺是合理的！</p>



<h2 id="那總體來說呢？">那總體來說呢？</h2>



<p>我們可以粗略的用簡單的「平均」的方式，將所有地區的房價平均起來：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">price_history.mean(axis=1).plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="918" height="540" src="http://34.96.136.135/wp-content/uploads/2020/07/dtsm.png" alt="dtsm" class="wp-image-979" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dtsm.png 918w, https://www.finlab.tw/wp-content/uploads/2020/07/dtsm-300x176.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/dtsm-768x452.png 768w" sizes="(max-width: 918px) 100vw, 918px" title="用程式分析房地產可行嗎？房價分析看這裡！ 6"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/dtsm.png"></a>就會看到2014年高峰後，台北市房價就處於下跌的狀態</p>



<h2 id="不是有人說2019年房價回升了嗎？">不是有人說2019年房價回升了嗎？</h2>



<p>刪除outlier的方式不同，可以得出不同的結論，<br>也是有一些實驗中 2019 年平均房價比 2018 年高，<br>所以目前也有很多人說止跌回升是有可能的</p>



<h2 id="但是">但是</h2>



<p>整體的大趨勢來說，從2014年以後開始下跌至今，似乎跌幅沒有像當初這麼重，甚至有止跌的跡象，但究竟房價有沒有回升，還需要謹慎評估。</p>



<p>甚至還有報導說，某些地段回到了2014、2015高點，這都是拿單一區段來當結論，見樹不見林的方式，背後居心自然是眾人皆知。</p>



<p>思考一下，為何現在都在推新建案？有很多節目，專家們都宣導，買公寓（舊房子）比較可能選到有壁癌、排水系統不好、貸款成數較低…等等，鼓吹大家買新建案。</p>



<p>當然他們很有可能是為了消費者著想，這些都是實話，不過也有可能是因為新房子比起公寓，更能賣出好價錢，進而維持房價不衰退，所以接下來我們就來分析一下：</p>



<h1 id="建物型態">建物型態</h1>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">building_type_prices = {}
for building_type in set(df['建物型態2']):
    cond = (
        (df['主要用途'] == '住家用')
        &amp; (df['單價元坪'] &lt; df["單價元坪"].quantile(0.8))
        &amp; (df['單價元坪'] > df["單價元坪"].quantile(0.2))
        &amp; (df['建物型態2'] == building_type)
        )
    building_type_prices[building_type] = df[cond]['單價元坪'].groupby(df[cond]['year']).mean().loc[2012:]
pd.DataFrame(building_type_prices)[['公寓', '住宅大樓', '套房', '華廈']].plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="918" height="540" src="http://34.96.136.135/wp-content/uploads/2020/07/tts.png" alt="tts" class="wp-image-980" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/tts.png 918w, https://www.finlab.tw/wp-content/uploads/2020/07/tts-300x176.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/tts-768x452.png 768w" sizes="(max-width: 918px) 100vw, 918px" title="用程式分析房地產可行嗎？房價分析看這裡！ 7"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/tts.png"></a>上圖中我們可以發現，老舊公寓的價格真的會不太理想，比起一般的大樓住宅或是住宅，近年價差越來越明顯，當價差到達一定的程度，買新房不一定比較好，舊房不一定這麼一無是處。</p>



<p>但是以平均來當作指標，其實也不是這麼精確，我們還是用分佈圖用眼睛來感受一下，才是最好瞭解房價的方法：</p>



<h2 id="分佈圖">分佈圖</h2>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">plt.rcParams['font.size'] = 20
for district in set(df['鄉鎮市區']):
    dfdistrict = df[df['鄉鎮市區'] == district]
    dfdistrict['單價元坪'][dfdistrict['單價元坪'] &lt; 2000000].hist(bins=120, alpha=0.7)

plt.xlim(0, 2000000)
plt.legend(set(df['鄉鎮市區']))</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="923" height="526" src="http://34.96.136.135/wp-content/uploads/2020/07/dhis.png" alt="dhis" class="wp-image-981" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dhis.png 923w, https://www.finlab.tw/wp-content/uploads/2020/07/dhis-300x171.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/dhis-768x438.png 768w" sizes="(max-width: 923px) 100vw, 923px" title="用程式分析房地產可行嗎？房價分析看這裡！ 8"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/dhis.png"></a>上圖中可以明顯感受到，不同地區房價的差異性，例如最右邊的分佈（大安區），大部分單價都比較貴，同時我們也可以看到一些低的詭譎的房價（每坪0萬？！），當然也有很多高的咋舌的單價，甚至一坪200萬都有，只能說富人的世界跟我們一般人還是差距很大呀！</p>



<p>我們取平均的時候無法將這些怪房價給濾除，但好在人眼可以，這就是分佈圖的重要性！</p>



<h2 id="買房使用-Python-簡單的範例">買房使用 Python 簡單的範例</h2>



<p>假如今天我們想在北投買房子，可以將北投的房價單獨拿出來看，並且按照建案型態製作分佈圖</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">dfdistrict = df[(df['鄉鎮市區'] == '北投區') &amp; (df['year'] >= 2018) &amp; (
    (df['建物型態2'] == '住宅大樓') | (df['建物型態2'] == '公寓') | (df['建物型態2'] == '套房')
)]
dfdistrict = dfdistrict[dfdistrict['單價元坪'] &lt; 2000000]

dfdistrict['單價元坪'].groupby(dfdistrict['建物型態2']).hist(bins=50, alpha=0.7)
plt.legend(set(dfdistrict['建物型態2']))</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="873" height="522" src="http://34.96.136.135/wp-content/uploads/2020/07/bhis.png" alt="bhis" class="wp-image-982" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/bhis.png 873w, https://www.finlab.tw/wp-content/uploads/2020/07/bhis-300x179.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/bhis-768x459.png 768w" sizes="(max-width: 873px) 100vw, 873px" title="用程式分析房地產可行嗎？房價分析看這裡！ 9"></figure></div>



<p><a href="https://www.finlab.tw/real-estate-analasys-histograms/bhis.png"></a>這樣我們就可以一眼看出來，究竟公寓跟電梯大樓有什麼不一樣，方便我們在選擇的時候，多一些考慮，讓我們在買房談價格時，可以有更全面的概念！</p>



<p>有時候買房不是為了投資，而是生活所需而不得不，在這個時代，我們已經無法買的精妙，炒房產呱呱叫，但至少在面對這種重大抉擇時，能有多一份數據輔佐，盡量不要「虧太多」，買到與價格相符的好房！</p>



<p>假如希望我們未來可以多多發佈類似的文章，<br>可以到<a href="https://www.facebook.com/finlab.python/" rel="noreferrer noopener" target="_blank">粉絲團</a>幫我們按個讚～！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/real-estate-analasys-histograms/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">972</post-id>	</item>
		<item>
		<title>台北最抗跌公寓在哪？ Python 告訴你 (Part 3)</title>
		<link>https://www.finlab.tw/real-state-best-district-old-buildings-taipei/</link>
					<comments>https://www.finlab.tw/real-state-best-district-old-buildings-taipei/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Mon, 20 Jul 2020 12:02:49 +0000</pubDate>
				<category><![CDATA[實價登入]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[房地產]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=1006</guid>

					<description><![CDATA[文末告訴你買哪裡？多少年齡的公寓？比較好！（結果滿出乎意料）根據專家的說法，由於人口老化的關係，房價未來比較悲觀，假如我們要自住買房怎麼辦？]]></description>
										<content:encoded><![CDATA[
<p>文末告訴你買哪裡？多少年齡的公寓？比較好<br>（結果滿出乎意料）<a href="https://www.finlab.tw/real-state-best-district-old-buildings-taipei/thumbnail.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="537" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-4-3-1024x537.png" alt="thumbnail 4 3" class="wp-image-1008" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-4-3-1024x537.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-4-3-300x157.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-4-3-768x403.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-4-3.png 1250w" sizes="(max-width: 1024px) 100vw, 1024px" title="台北最抗跌公寓在哪？ Python 告訴你 (Part 3) 10"></figure></div>



<p>根據專家的說法，<br>由於人口老化的關係，<br>房價未來比較悲觀，<br>假如我們要自住買房怎麼辦？</p>



<p>有一個家的好處是<br>畢竟每個人生活習慣不同<br>要客制化專屬的生活空間<br>只有擁有房子才辦得到</p>



<p>但是，房子保值的年代已經過去了<br>幾年前台北的房價被炒的很高<br>有些地方明顯就是太貴了<br>這幾年慢慢的顯現出來</p>



<p>假如你因為工作需求，<br>需要再台北買一個自己的家<br>要買在哪裡比較保值？<br>對，保值而已，不求上漲，<br>只求不要跌太多</p>



<p>打開實價登錄，<br>不論是<a href="https://price.houseprice.tw/" rel="noreferrer noopener" target="_blank">實價網</a>、<a href="https://www.rakuya.com.tw/realprice/result" rel="noreferrer noopener" target="_blank">樂屋網</a>、或其他的網站<br>在功能上，都有很多進步的空間！</p>



<p>原因在於這些網站，都只提供進幾年的數據，<br>而且無法統計以及繪圖功能，<br>大部分都只提供近一兩年的比較，<br>但一般人自住買房，關心的是10年後的房價，<br>而不只是短期的上漲下跌，</p>



<p>所以只好來用 Python 做一點功課了</p>



<p>首先，我們根據之前的介紹，我們可以</p>



<ul><li><a href="https://www.finlab.tw/real-estate-analysis1/">爬到實價登錄歷史資料</a></li><li><a href="https://www.finlab.tw/real-estate-analasys-histograms/">將資料整理成 dataframe</a><br>可以先進行以上兩個步驟，然後就可以接著來寫 code 囉！</li></ul>



<p>這次的主題是「公寓」<br>我們假設某個人想買台北的房子<br>但是由於金錢考量，只能選擇公寓，</p>



<p>「究竟要怎麼選呢？」</p>



<h3 id="1-用區域來選">1. 用區域來選</h3>



<p>首先我們想要算出，台北市「公寓」的歷史走勢</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">
import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = ['Noto Sans TC Regular'] # 有支援中文的字體
# plt.rcParams['axes.unicode_minus']=False

result = {}
    
for dis in set(df['鄉鎮市區']):

    condition1 = df['土地區段位置/建物區段門牌'].notna()
    condition2 = df['建物型態'] == '公寓(5樓含以下無電梯)'
    condition3 = (df['鄉鎮市區'] == dis)
    
    df_local = df[condition3 &amp; condition2]
    result[dis] = df_local['單價元坪'].groupby(df_local.index.year).mean()

k = pd.DataFrame(result).loc[2012:]
k.plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="624" height="374" src="http://34.96.136.135/wp-content/uploads/2020/07/dis-price.png" alt="dis price" class="wp-image-1009" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dis-price.png 624w, https://www.finlab.tw/wp-content/uploads/2020/07/dis-price-300x180.png 300w" sizes="(max-width: 624px) 100vw, 624px" title="台北最抗跌公寓在哪？ Python 告訴你 (Part 3) 11"></figure></div>



<p><a href="https://www.finlab.tw/real-state-best-district-old-buildings-taipei/dis-price.png"></a>這張圖可以看出很多端倪，<br>自從政府調整法令後，<br>炒房投資客有明顯減少，<br>所以貴的地區房價下跌，</p>



<p>另外也因為台北交通便利，<br>（ubike、公車、捷運）<br>所以不論房價高低，<br>其實生活便利程度相差無幾<br>使得便宜的區域房價上漲，</p>



<p>房價差距越來越小，<br>平均來說，各區房價都越來越往 50～60W/坪 的價格趨近。</p>



<p>另外，除了肉眼來判別外，<br>我們也可以由價格標準差得知，<br>台北市各區，房價差異慢慢縮小，<a href="https://www.finlab.tw/real-state-best-district-old-buildings-taipei/std.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="629" src="http://34.96.136.135/wp-content/uploads/2020/07/std-1024x629.png" alt="std" class="wp-image-1010" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/std-1024x629.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/std-300x184.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/std-768x471.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/std.png 1124w" sizes="(max-width: 1024px) 100vw, 1024px" title="台北最抗跌公寓在哪？ Python 告訴你 (Part 3) 12"></figure></div>



<p>由上面的分析，我們可以得到一個結論<br>對於「公寓」而言，我們應該選擇<br>「房價目前較低的地區」<br>因為這些地區的房價，會慢慢往平均（50～60W/坪）移動</p>



<p>所以假如是買公寓的話，要選哪裡呢？<br>以條件來分析的話，可以這樣選</p>



<ol><li>2019年房價 &lt; 平均的區域（價格低的）</li><li>2019年房價 &gt; 2018年的區域（看近期有漲的）</li></ol>



<p>以下就是程式碼以及選出來的區域：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">p2019 = k.loc[2019]
p2018 = k.loc[2018]

good_district = (p2019 > p2018) &amp; (p2019 &lt; p2019.mean())
p2019[good_district].sort_values()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="360" src="http://34.96.136.135/wp-content/uploads/2020/07/ranking-1024x360.png" alt="ranking" class="wp-image-1011" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/ranking-1024x360.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/ranking-300x105.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/ranking-768x270.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/ranking.png 1162w" sizes="(max-width: 1024px) 100vw, 1024px" title="台北最抗跌公寓在哪？ Python 告訴你 (Part 3) 13"></figure></div>



<p><a href="https://www.finlab.tw/real-state-best-district-old-buildings-taipei/ranking.png"></a></p>



<p>2. 用屋齡來判斷</p>



<p>大家都說越老的房子會折舊，<br>越老的房子應該要越便宜，<br>然而事情真的是這樣嗎？</p>



<p>實價登錄的資料令人跌破眼鏡！<br>根據數據的分析，每個區域都適用：<br>在屋齡「40～50」年的房子，竟然還會往上漲！？<br>不論哪一個區域，哪一年，都可以觀察到這個現象！</p>



<p>舉個例子，以下我們來繪圖「中山區」房價</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">for i in range(2012, 2020):
    df_local = df[(df['鄉鎮市區'] == '中山區') &amp; (df['year'] == i)]
    df_local['房屋年齡'] = ((2019 - pd.to_numeric(df_local['建築完成年月'].str[:3]) - 1911)).astype(int, errors='ignore')
    df_local['房屋年齡'] = pd.to_numeric(df_local['房屋年齡'] /10).round()
    df_local['單價元坪'].groupby(df_local['房屋年齡']).mean().plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="401" height="266" src="http://34.96.136.135/wp-content/uploads/2020/07/age.png" alt="age" class="wp-image-1012" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/age.png 401w, https://www.finlab.tw/wp-content/uploads/2020/07/age-300x199.png 300w" sizes="(max-width: 401px) 100vw, 401px" title="台北最抗跌公寓在哪？ Python 告訴你 (Part 3) 14"></figure></div>



<p><a href="https://www.finlab.tw/real-state-best-district-old-buildings-taipei/age.png"></a></p>



<p>上圖每一條線的意思是<br>每一年「不同屋齡的公寓價格」<br>我們可以觀察到幾個現象：</p>



<ol><li>年輕的公寓下跌的比較快</li><li>老公寓在40～50歲時，還會往上漲</li></ol>



<p>第二點尤其的詭異，<br>可能是因為都更的關係？<br>不只是中山區，<br>台北的任一區都可以觀察到這個現象<br>大家可以回去跑跑看</p>



<p>總之，至少老公寓折舊的速度，<br>是比想像中還要慢的，<br>（或可能是老公寓都賣不出去，除<br>非特別地段、特別地點，導致樣本有偏差？<br>還需要再研究）</p>



<h3 id="總結">總結</h3>



<p>這邊還是要提醒大家，<br>這個是針對「老公寓」做的研究，<br>假如是「新成屋」、「華廈」等屋型，<br>還需要額外去研究才能更清楚。</p>



<p>數據只是一部分的幫助，<br>買到好房子還是必需要做很多其他功課<br>例如：附近的捷運、生活機能、嫌惡設施 等等<br>都很重要<br>另外，看房技巧、談價格 也不能忽略，</p>



<p>不過以統計的觀點來說</p>



<p>保值地區首選順序：<br>「萬華區、文山區、內湖區、南港區」</p>



<p>保值屋齡首選：<br>「20～40歲以上」公寓</p>



<p>假如你要在台北買房，<br>而且想要買舊公寓重新裝潢，<br>希望這篇文章可以幫助到你！</p>



<p>也歡迎分享給有在看房的朋友哦！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/real-state-best-district-old-buildings-taipei/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1006</post-id>	</item>
	</channel>
</rss>
