所以就先簡單的找個例子來做實驗
想知道台北市各個行政區的房價是不是跟性侵案件的多寡有正相關
我們要從從Data.Taipei取得婦幼安全相關的資料
跟內政部提供的實價登錄資料
兩個資料我都取2017年第一季跟第二季加在一起來實驗
先處理房價部分的資料
我先用excel將第一季跟第二季的資料做結合
接下來用python去解析資料
import pandas去做讀取csv
import pandas
from pandas import DataFrame
df = pandas.read_csv('/Users/Chou/Downloads/2017S1/A_lvr_land_A1_S1+S2.CSV',encoding = "big5") # 要encodig成big5不然會有亂碼
萬一遇到UnicodeDecodeError的問題
因為資料裡面可能含有一些不乾淨的byte
使用iconv講檔案處理乾淨
iconv -c -f big5 -t big5 A_lvr_land_A.CSV > A_lvr_land_A2.CSV這時候把df印出來應該可以得到如下的資料
接下來可以看到交易標的的欄位有一些是單純的車位販售,引此不列入我們這次要抓取的資料
dfWithoutCar = df[df['交易標的'] != '汽車']然後將所有的行政區集合起來並且算出平均數還有資料數量
groupByCity = dfWithoutCar.groupby(["鄉鎮市區"]).agg(['mean', 'count'])
將取出來的資料從高排到低
再將所有的mean(也就是我們要用的價錢)乘上3,因為資料提供的單位是每平方公尺,轉換成每坪的價錢比較好讀
orderByPricePerPing = DataFrame(orderByPrcingPerMPlus)
for index, row in orderByPrcingPerMPlus.items():
if(index == 'mean'):
for i,price in row.items():
orderByPricePerPing[index][i] = price * 3
orderByPricePerPing
將最後需要的資料取出來就是我們要用的北市行政區的資料了

