Pythonを使って東京都家賃についての研究 (3の3)

#結果抜粋
ブログに同時掲載：
https://leoluistudio.com/blog/42/python%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e6%9d%b1%e4%ba%ac%e9%83%bd%e5%ae%b6%e8%b3%83%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6%e3%81%ae%e7%a0%94%e7%a9%b6-4%e3%81%ae3/

***

#統計の手法2

##プログラムと結果

###建物ごとに中央値を計算する

理由としては、建物の物件数が違います。例えば1ビル50物件があれば、分布を計算する時、その重みが1ビル1物件より50倍になります。均等に計算するように、建物ごとに中央値を取ります。

```python
conn = sqlite3.connect(‘info.db’)
df = pd.read_sql_query(“SELECT pid,price,area FROM price”, conn)
conn.close()

df[‘municipal’] = df.apply(municipal, axis=1)
dfmedian = df.groupby([‘pid’, ‘municipal’])[‘price’, ‘area’, ‘average’].median()
dfmedian_reset = dfmedian.reset_index(level=’municipal’)
```

***

区ごとに価格の分布（23区）

```python
# グラフ
fig = go.Figure()
for i in m23_list:
    dfgroup = dfmedian_reset[dfmedian_reset[‘municipal’] == i]
    fig.add_trace(go.Box(x=dfgroup[‘price’], y=dfgroup[‘municipal’], boxpoints=False))
fig.update_traces(orientation=’h’, showlegend=False)
fig.update_xaxes(range=[0, 500000])
```

![L2no2A.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/0248bee5-1804-9943-96c5-0f9d8ce771ac.png)

***

区ごとに面積の分布（23区）

```python
# グラフ
fig = go.Figure()
for i in m23_list:
    dfgroup = dfmedian_reset[dfmedian_reset[‘municipal’] == i]
    fig.add_trace(go.Box(x=dfgroup[‘area’], y=dfgroup[‘municipal’], boxpoints=False))
fig.update_traces(orientation=’h’, showlegend=False)
fig.update_xaxes(range=[0, 100])
```

![L2no2B.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/ce596f5f-0b78-d08d-bd19-70df045cce42.png)

***

平均価格（価格 / 面積）を計算する

```python
def average(df):
    return int(df[‘price’] / df[‘area’])

df[‘average’] = df.apply(average, axis=1)
```

***

区ごとに平均価格の分布（23区）

```python
# グラフ
fig = go.Figure()
for i in m23_list:
    dfgroup = dfmedian_reset[dfmedian_reset[‘municipal’] == i]
    fig.add_trace(go.Box(x=dfgroup[‘average’], y=dfgroup[‘municipal’], boxpoints=False))
fig.update_traces(orientation=’h’, showlegend=False)
```

![L2no2D.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/b67905b1-6c36-ae23-ccf7-cf937dd50aa6.png)

***

平均価格と駅まで徒歩の時間の関係

```python
# Dataframeの処理
dfmedian_reset = dfmedian.reset_index(level=’pid’)
dfmedian_reset[‘train’] = dfmedian_reset.apply(trainminute, axis=1)

# グラフ
fig = px.scatter(dfmedian_reset, x=’train’, y=’average’, height=500, width=1000)
```

![L2no3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/570abe6c-1130-b92e-3937-3e9ae4abd6a3.png)

***

平均価格と建物のタイプの関係

```python
dfmedian_reset = dfmedian.reset_index(level=’pid’)
dfmedian_reset[‘type’] = dfmedian_reset.apply(buildtype, axis=1)
fig = go.Figure()
for i in ([‘マンション’, ‘アパート’]):
    dfgroup = dfmedian_reset[dfmedian_reset[‘type’] == i]
    fig.add_trace(go.Box(x=dfgroup[‘price’], y=dfgroup[‘type’], boxpoints=False))
fig.update_traces(orientation=’h’, showlegend=False)
fig.update_xaxes(range=[0, 500000])
fig.update_layout(height=500, width=1000)
```

![L2no4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/48d14cb4-d877-cbb6-2772-690da1f6550c.png)

***

##その他の結果

平均価格と建物のタイプの関係
![L2no5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/c30bdc68-1b53-4db8-c93a-f7fac89ae451.png)

***

平均価格と建物構造の関係
![L2no6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/f99aefa0-9acd-5c0d-a260-84b210f024f8.png)

***

平均価格と駐車場の関係
![L2no7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/e94b492a-07b3-d4ba-c9d5-b2f6fd39edf8.png)

***

平均価格と駐車場の関係
![L2no8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/16321b1c-c178-ba9b-688d-4d8782400722.png)

***

完成日の分布（23区と市部に分ける）
![L2no9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/20c60915-319b-0dca-a216-31b96b5cba99.png)

***

方向と区市町村の価格ヒートマップ
![L3no1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/893830/3f670b07-40a5-5f38-dc04-61acc46d422f.png)