天天看點

18.scrapy_maitian_analysis

1_info.py

# encoding: utf-8
import pandas as pd

# 租房 基本資訊
# 讀取檔案 df=dataframe
df = pd.read_json("zufang.json")
# print(df)
# print(df.columns)

# 使用pandas的describe方法,列印基本資訊
print(df.describe())
# 按照區,分别統計個數
print(df["district"].value_counts())
# print('**************************')
# # 二手房 基本資訊
df = pd.read_json("ershoufang.json")
print(df.describe())
# 分别統計個數
print(df["district"].value_counts())           

複制

2_pie_chart.py

# coding:utf-8
import numpy as np
import pandas as pd
import json
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

myfont = FontProperties(
    fname='/Users/seancheney/.matplotlib/mpl-data/fonts/ttf/SimHei.ttf')

labels = '朝陽', '海澱', '昌平', '東城', '大興', '西城', '豐台', '石景山', '通州', '順義'

df_zf = pd.read_json("ershoufang.json")
chaoyang_count = df_zf['district'].value_counts()['朝陽']
haidian_count = df_zf['district'].value_counts()['海澱']
changping_count = df_zf['district'].value_counts()['昌平']
dongcheng_count = df_zf['district'].value_counts()['東城']
daxing_count = df_zf['district'].value_counts()['大興']
xicheng_count = df_zf['district'].value_counts()['西城']
fengtai_count = df_zf['district'].value_counts()['豐台']
shijingshan_count = df_zf['district'].value_counts()['石景山']
tongzhou_count = df_zf['district'].value_counts()['通州']
shunyi_count = df_zf['district'].value_counts()['順義']

sizes = [
    chaoyang_count,
    haidian_count,
    changping_count,
    dongcheng_count,
    daxing_count,
    xicheng_count,
    fengtai_count,
    shijingshan_count,
    tongzhou_count,
    shunyi_count]
explode = (0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
plt.subplot(121)
plt.pie(
    sizes,
    explode=explode,
    labels=labels,
    autopct='%1.1f%%',
    shadow=True,
    startangle=-90)
plt.axis('equal')
plt.title("房屋出售分布", fontproperties=myfont)

labels = '朝陽', '海澱', '昌平', '東城', '大興', '西城', '豐台', '石景山', '通州', '順義'
df_zf = pd.read_json("zufang.json")
chaoyang_count = df_zf['district'].value_counts()['朝陽']
haidian_count = df_zf['district'].value_counts()['海澱']
changping_count = df_zf['district'].value_counts()['昌平']
dongcheng_count = df_zf['district'].value_counts()['東城']
daxing_count = df_zf['district'].value_counts()['大興']
xicheng_count = df_zf['district'].value_counts()['西城']
fengtai_count = df_zf['district'].value_counts()['豐台']
shijingshan_count = df_zf['district'].value_counts()['石景山']
tongzhou_count = df_zf['district'].value_counts()['通州']

labels = '朝陽', '海澱', '昌平', '東城', '大興', '西城', '豐台', '石景山', '通州'
sizes = [
    chaoyang_count,
    haidian_count,
    changping_count,
    dongcheng_count,
    daxing_count,
    xicheng_count,
    fengtai_count,
    shijingshan_count,
    tongzhou_count]
explode = (0.1, 0, 0, 0, 0, 0, 0, 0, 0)
plt.subplot(122)
plt.pie(
    sizes,
    explode=explode,
    labels=labels,
    autopct='%1.1f%%',
    shadow=True,
    startangle=-90)
plt.axis('equal')
plt.title("房屋出租分布", fontproperties=myfont)
plt.rc('font', family=['SimHei'])
plt.show()           

複制

3_hist.py

import numpy as np
import pandas as pd
import json
import matplotlib.pyplot as plt
from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei']

df = pd.read_json("ershoufang.json")

print(df.columns)

unitprice_values = df.unitprice
plt.hist(unitprice_values,bins=25)
plt.xlim(0, 200000)
plt.title(u"房屋出售每平米價格分布")
plt.xlabel(u'價格(機關:萬/平方米)')
plt.ylabel(u'套數')
plt.show()           

複制

4_ratio.py

# 售租比
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

district = ('西城', '石景山', '東城', '海澱', '豐台', '昌平', '大興', '朝陽', '通州')

# 讀取租房資料
df_zf = pd.read_json("zufang.json")
unitprice_zf = df_zf['price'] / df_zf['area']
df_zf['unitprice'] = unitprice_zf

# print(df_zf)

month_price = df_zf.groupby(by=['district']).sum(
)['unitprice'] / df_zf["district"].value_counts()

# print(month_price)

# # 讀取二手房資料
df_esf = pd.read_json("ershoufang.json")

sell_price = df_esf.groupby(by=['district']).sum(
)['unitprice'] / df_esf["district"].value_counts()

# print(sell_price)

xicheng_ratio = sell_price['西城'] / month_price['西城']
shijingshan_ratio = sell_price['石景山'] / month_price['石景山']
dongcheng_ratio = sell_price['東城'] / month_price['東城']
haidian_ratio = sell_price['海澱'] / month_price['海澱']
fengtai_ratio = sell_price['豐台'] / month_price['豐台']
changping_ratio = sell_price['昌平'] / month_price['昌平']
daxing_ratio = sell_price['大興'] / month_price['大興']
chaoyang_ratio = sell_price['朝陽'] / month_price['朝陽']
tongzhou_ratio = sell_price['通州'] / month_price['通州']
#
#
ratio = (
    xicheng_ratio,
    shijingshan_ratio,
    dongcheng_ratio,
    haidian_ratio,
    fengtai_ratio,
    changping_ratio,
    daxing_ratio,
    chaoyang_ratio,
    tongzhou_ratio
)

fig, ax = plt.subplots()

y_pos = np.arange(len(district))
# performance = ratio

ax.barh(y_pos, ratio, align='center', color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(district)
# ax.invert_yaxis()
ax.set_xlabel('售租比(機關:月)')
ax.set_title('各區房屋售租比')

plt.show()           

複制