通过爱数科下载中国租房信息数据集
一、选题背景
当前,住房问题是重要的民生问题,是每个人基本生活保障的前提,也是家庭和睦,社会稳定的关键。但我国从2003年以来房价飞速增长,居高不下,买房成为许多人,特别是中低收入家庭的奢望。面对高额房价,他们也只能望而兴叹。对于中低收入家庭而言,他们没有能力通过市场经济来解决住房问题,只能通过租房来解决住房问题。
二、大数据分析设计方案
1.本数据集的数据内容与数据特征分析
本数据集是通过爱数科下载的中国租房信息数据集,本数据集包含2020末-2021年初,来自房天下、58同城、赶集网的位于北京、上海、深圳约20000条数据,非常适合探索性数据分析,有近万行数据可供使用,且为表格数据。
2.数据分析的课程设计方案概述
(1)通过爱数科下载中国租房信息数据集。
(2)导入库和数据集,对中国租房信息数据集数据分析及进行可视化,保存于电脑;为了能够更加直观地看到不同因素租房数据差异,做表查看;进行数据清洗,查看平均房租以及房价前十名。以及各种统计分析并将他们进行可视化处理。
三、数据分析步骤
1.数据源
数据来源:C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv
2.数据清洗
(1)
1 import matplotlib.pyplot as plt 2 import numpy as np 3 import pandas as pd 4 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 5 df.head()
(2)了解平均房租
1 #查看平均房租 2 df['价格(元/㎡)'] = df['价格']/df['面积'] 3 # 使用round函数来保留位小数 4 print("平均房租",round(df.loc[:,'价格(元/㎡)'].mean(),2)) 5 print("最高房租",round(df.loc[:,'价格(元/㎡)'].max(),2)) 6 print("最低房租",round(df.loc[:,'价格(元/㎡)'].min(),2))
(3)了解房价前十名
1 #查看价格top10 2 house_rent = set(df["价格"]) # 统计价格数量 3 # print(house_rent) 4 5 # 创建一个DataFrame 对象,筛选需要数据{价格,价格数量} 6 d_rent = pd.DataFrame({"租金":df["价格"].unique(),"价格数量":[0]*len(house_rent) }) 7 print(d_rent) 8 print('-'*40) 9 10 # groupby统计价格数量,并从大到小排序 11 groupby_area = df.groupby(by="价格").count() 12 print(groupby_area) 13 d_rent["价格数量"] = groupby_area.values 14 d_rent = d_rent.sort_values(by=["价格数量"],ascending=False) 15 print(d_rent) 16 d_top10 = d_rent.head(10) # 取前十 17 print("价格数量TOP10是:\n",d_top10)
(4)了解最贵小区
1 from pyecharts.charts import Bar, Pie 2 import pyecharts.options as opts 3 df['价格(元/㎡)'] = df['价格']/df['面积'] 4 expensive_flat = pd.Series(df['价格(元/㎡)'].nlargest(10)) 5 6 flat_name = [] 7 for i in expensive_flat.index: 8 flat_name.append(df['小区'][i]) 9 10 flat_values = [] 11 for value in expensive_flat.values: 12 flat_values.append(round(value, 1)) 13 14 bar2 = ( 15 Bar() 16 .add_xaxis(flat_name) 17 .add_yaxis('单位平方价格', flat_values) 18 .reversal_axis() 19 .set_series_opts(label_opts=opts.LabelOpts(position="right")) 20 .set_global_opts(title_opts=opts.TitleOpts(title='最贵小区Top10'), 21 xaxis_opts=opts.AxisOpts(name_rotate=60, axislabel_opts={"rotate": 45})) 22 ) 23 bar2.render("最贵小区Top10.html")
3.数据可视化
(1)通过“室”的数量查看租房信息
1 from pylab import * 2 mpl.rcParams['font.sans-serif'] = ['SimHei'] 3 mpl.rcParams['axes.unicode_minus'] = False 4 df = (df['室'].value_counts())[:16].to_frame() 5 plt.figure(figsize=(15,15)) 6 plt.pie(df['室'], labels=df.index.values, autopct='%.1f%%') 7 plt.title('中国租房信息',fontsize=20)
(2)通过“卫”的数量查看租房信息
1 from pylab import * 2 mpl.rcParams['font.sans-serif'] = ['SimHei'] 3 mpl.rcParams['axes.unicode_minus'] = False 4 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 5 df = (df['卫'].value_counts())[:16].to_frame() 6 plt.figure(figsize=(15,15)) 7 plt.pie(df['卫'], labels=df.index.values, autopct='%.1f%%') 8 plt.title('中国租房信息',fontsize=20)
(3)通过“厅”的数量查看租房信息
1 from pylab import * 2 mpl.rcParams['font.sans-serif'] = ['SimHei'] 3 mpl.rcParams['axes.unicode_minus'] = False 4 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 5 df = (df['厅'].value_counts())[:16].to_frame() 6 plt.figure(figsize=(15,15)) 7 plt.pie(df['厅'], labels=df.index.values, autopct='%.1f%%') 8 plt.title('中国租房信息',fontsize=20)
(4)为了能够更加直观地看到户型数量间的差异,做表查看。
1 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 2 new_df=pd.DataFrame({'区':df['区'].unique(), 3 '数量':[0]*len(df['区'].unique())}) 4 groupy_area=df.groupby(by='区').count() 5 new_df['数量']=groupy_area.values 6 # 按“数量”一列从大到小排列 7 new_df.sort_values(by=['数量'],ascending=False) 8 #new_df 9 # 定义函数,用来计算各户型数量 10 def all_house(arr): 11 arr=np.array(arr) 12 key=np.unique(arr) 13 result={} 14 for k in key: 15 mask=(arr==k) 16 arr_new=arr[mask] 17 v=arr_new.size 18 result[k]=v 19 return result 20 # 获取户型数据 21 house_array=df['室'] 22 house_info=all_house(house_array) 23 house_info 24 # 使用字典推导式 25 house_type=dict((key,value) for key,value in house_info.items() if value>50) 26 show_houses=pd.DataFrame({'室':[x for x in house_type.keys()], 27 '数量':[x for x in house_type.values()]}) 28 print(show_houses)
(5)统计价格可视化
1 house_rent = set(df["价格"]) # 统计多少个价格 2 # print(house_rent) 3 4 # 创建一个DataFrame 对象,筛选需要数据{价格,价格数量} 5 d_rent = pd.DataFrame({"租金":df["价格"].unique(),"价格数量":[0]*len(house_rent) }) 6 print(d_rent) 7 print('-'*40) 8 9 # groupby统计价格数量,并从大到小排序 10 groupby_area = df.groupby(by="价格").count() 11 print(groupby_area) 12 d_rent["价格数量"] = groupby_area.values 13 d_rent = d_rent.sort_values(by=["价格数量"],ascending=False) 14 print(d_rent) 15 d_top10 = d_rent.head(10) # 取前十 16 print("价格数量TOP10是:\n",d_top10)
(6)总面积与总价的关系散点图
1 home_area = df['面积'].apply(lambda x:float(x)) 2 # print(home_area.head()) 3 total_price = df['价格'] 4 # print(total_price.head()) 5 plt.scatter(home_area,total_price,s=3) 6 plt.title('中国租房情况',fontsize=15) 7 plt.xlabel('房屋面积',fontsize=15) 8 plt.ylabel('房价',fontsize=15) 9 plt.grid(linestyle=":", color="r") 10 plt.show()
(7)居室与价格的关系
1 import pandas as pd 2 import numpy as np 3 import matplotlib.pyplot as plt 4 import seaborn as sns 5 6 #查看室与价格之间的关系 7 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 8 fig,ax = plt.subplots() 9 fig.set_size_inches(9,7) 10 sns.boxplot(x=df['室'],y=df['价格'],data=df) 11 plt.show() 12 13 #分别查看1,2,3,4室下面积与价格之间的关系 14 fig,ax = plt.subplots(2,2) 15 ax1 = ax[0,0] 16 ax2 = ax[0,1] 17 ax3 = ax[1,0] 18 ax4 = ax[1,1] 19 axes = [ax1,ax2,ax3,ax4] 20 fig.set_size_inches(12,12) 21 dt = df[df['室']==1] 22 dt_1 = df[df['室']==2] 23 dt_2 = df[df['室']==3] 24 dt_3 = df[df['室']==4] 25 dts = [dt,dt_1,dt_2,dt_3] 26 for a,b in zip(dts,axes): 27 sns.scatterplot(x='面积',y='价格',data=a,ax=b) 28 plt.show()
(8)查看室,卫,厅下面积与价格之间的关系
1 fig,ax = plt.subplots(2,2) 2 ax1 = ax[0,0] 3 ax2 = ax[0,1] 4 ax3 = ax[1,0] 5 axes = [ax1,ax2,ax3,] 6 fig.set_size_inches(12,12) 7 dt = df[df['室']==1] 8 dt_1 = df[df['卫']==2] 9 dt_2 = df[df['厅']==3] 10 dts = [dt,dt_1,dt_2,] 11 for a,b in zip(dts,axes): 12 sns.scatterplot(x='面积',y='价格',data=a,ax=b) 13 plt.show()
(9)查看各城市下待租房间数量
1 fig,ax = plt.subplots() 2 fig.set_size_inches(10,10) 3 xq = df['区'].value_counts().sort_values() 4 index = list(xq.index) 5 value = list(xq.values) 6 qx = pd.DataFrame({'区':index,'城市':value}) 7 qx.plot.barh(x='区',y='城市',ax=ax,color='blue',fontsize=12) 8 plt.legend(loc='right') 9 for a,b in zip(value,np.arange(0,14,1)): 10 plt.text(a+0.5,b,a,fontsize=12) 11 plt.show()
1 import numpy as np 2 import matplotlib.pyplot as plt 3 from mpl_toolkits. mplot3d import Axes3D 4 fig = plt.figure() 5 ax = Axes3D(fig,auto_add_to_figure=False) 6 fig.add_axes(ax) 7 x = df['价格'] 8 y = df['最近学校距离'] 9 z = df['周边学校个数'] 10 ax. scatter(x, y, z) 11 ax.set_xlabel('价格') 12 ax.set_ylabel( '最近学校距离') 13 ax.set_zlabel('周边学校个数') 14 plt.show()
1 import numpy as np 2 import matplotlib.pyplot as plt 3 from mpl_toolkits. mplot3d import Axes3D 4 fig = plt.figure() 5 ax = Axes3D(fig,auto_add_to_figure=False) 6 fig.add_axes(ax) 7 x = df['价格'] 8 y = df['最近医院距离'] 9 z = df['周边医院个数'] 10 ax. scatter(x, y, z) 11 ax.set_xlabel('价格') 12 ax.set_ylabel('最近医院距离') 13 ax.set_zlabel('周边医院个数') 14 plt.show()
4.附完整程序源代码
1 import matplotlib.pyplot as plt 2 import numpy as np 3 import pandas as pd 4 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 5 df.head() 6 #查看平均房租 7 df['价格(元/㎡)'] = df['价格']/df['面积'] 8 # 使用round函数来保留位小数 9 print("平均房租",round(df.loc[:,'价格(元/㎡)'].mean(),2)) 10 print("最高房租",round(df.loc[:,'价格(元/㎡)'].max(),2)) 11 print("最低房租",round(df.loc[:,'价格(元/㎡)'].min(),2)) 12 #查看价格top10 13 house_rent = set(df["价格"]) # 统计价格数量 14 # print(house_rent) 15 16 # 创建一个DataFrame 对象,筛选需要数据{价格,价格数量} 17 d_rent = pd.DataFrame({"租金":df["价格"].unique(),"价格数量":[0]*len(house_rent) }) 18 print(d_rent) 19 print('-'*40) 20 21 # groupby统计价格数量,并从大到小排序 22 groupby_area = df.groupby(by="价格").count() 23 print(groupby_area) 24 d_rent["价格数量"] = groupby_area.values 25 d_rent = d_rent.sort_values(by=["价格数量"],ascending=False) 26 print(d_rent) 27 d_top10 = d_rent.head(10) # 取前十 28 print("价格数量TOP10是:\n",d_top10) 29 from pyecharts.charts import Bar, Pie 30 import pyecharts.options as opts 31 df['价格(元/㎡)'] = df['价格']/df['面积'] 32 expensive_flat = pd.Series(df['价格(元/㎡)'].nlargest(10)) 33 34 flat_name = [] 35 for i in expensive_flat.index: 36 flat_name.append(df['小区'][i]) 37 38 flat_values = [] 39 for value in expensive_flat.values: 40 flat_values.append(round(value, 1)) 41 42 bar2 = ( 43 Bar() 44 .add_xaxis(flat_name) 45 .add_yaxis('单位平方价格', flat_values) 46 .reversal_axis() 47 .set_series_opts(label_opts=opts.LabelOpts(position="right")) 48 .set_global_opts(title_opts=opts.TitleOpts(title='最贵小区Top10'), 49 xaxis_opts=opts.AxisOpts(name_rotate=60, axislabel_opts={"rotate": 45})) 50 ) 51 bar2.render("最贵小区Top10.html") 52 from pylab import * 53 mpl.rcParams['font.sans-serif'] = ['SimHei'] 54 mpl.rcParams['axes.unicode_minus'] = False 55 df = (df['室'].value_counts())[:16].to_frame() 56 plt.figure(figsize=(15,15)) 57 plt.pie(df['室'], labels=df.index.values, autopct='%.1f%%') 58 plt.title('中国租房信息',fontsize=20) 59 from pylab import * 60 mpl.rcParams['font.sans-serif'] = ['SimHei'] 61 mpl.rcParams['axes.unicode_minus'] = False 62 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 63 df = (df['卫'].value_counts())[:16].to_frame() 64 plt.figure(figsize=(15,15)) 65 plt.pie(df['卫'], labels=df.index.values, autopct='%.1f%%') 66 plt.title('中国租房信息',fontsize=20) 67 from pylab import * 68 mpl.rcParams['font.sans-serif'] = ['SimHei'] 69 mpl.rcParams['axes.unicode_minus'] = False 70 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 71 df = (df['厅'].value_counts())[:16].to_frame() 72 plt.figure(figsize=(15,15)) 73 plt.pie(df['厅'], labels=df.index.values, autopct='%.1f%%') 74 plt.title('中国租房信息',fontsize=20) 75 #为了能够更加直观地看到户型数量间的差异,做表查看。 76 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 77 new_df=pd.DataFrame({'区':df['区'].unique(), 78 '数量':[0]*len(df['区'].unique())}) 79 groupy_area=df.groupby(by='区').count() 80 new_df['数量']=groupy_area.values 81 # 按“数量”一列从大到小排列 82 new_df.sort_values(by=['数量'],ascending=False) 83 #new_df 84 # 定义函数,用来计算各户型数量 85 def all_house(arr): 86 arr=np.array(arr) 87 key=np.unique(arr) 88 result={} 89 for k in key: 90 mask=(arr==k) 91 arr_new=arr[mask] 92 v=arr_new.size 93 result[k]=v 94 return result 95 # 获取户型数据 96 house_array=df['室'] 97 house_info=all_house(house_array) 98 house_info 99 # 使用字典推导式 100 house_type=dict((key,value) for key,value in house_info.items() if value>50) 101 show_houses=pd.DataFrame({'室':[x for x in house_type.keys()], 102 '数量':[x for x in house_type.values()]}) 103 print(show_houses) 104 house_rent = set(df["价格"]) # 统计多少个价格 105 # print(house_rent) 106 107 # 创建一个DataFrame 对象,筛选需要数据{价格,价格数量} 108 d_rent = pd.DataFrame({"租金":df["价格"].unique(),"价格数量":[0]*len(house_rent) }) 109 print(d_rent) 110 print('-'*40) 111 112 # groupby统计价格数量,并从大到小排序 113 groupby_area = df.groupby(by="价格").count() 114 print(groupby_area) 115 d_rent["价格数量"] = groupby_area.values 116 d_rent = d_rent.sort_values(by=["价格数量"],ascending=False) 117 print(d_rent) 118 d_top10 = d_rent.head(10) # 取前十 119 # 绘制总面积和总价的散点关系图 120 home_area = df['面积'].apply(lambda x:float(x)) 121 # print(home_area.head()) 122 total_price = df['价格'] 123 # print(total_price.head()) 124 plt.scatter(home_area,total_price,s=3) 125 plt.title('中国租房情况',fontsize=15) 126 plt.xlabel('房屋面积',fontsize=15) 127 plt.ylabel('房价',fontsize=15) 128 plt.grid(linestyle=":", color="r") 129 plt.show() 130 #查看居室与价格之间的关系 131 import pandas as pd 132 import numpy as np 133 import matplotlib.pyplot as plt 134 import seaborn as sns 135 136 #查看室与价格之间的关系 137 df=pd.read_csv('C:/Users/bb/python-jupyter noteboook/中国租房信息数据集.csv') 138 fig,ax = plt.subplots() 139 fig.set_size_inches(9,7) 140 sns.boxplot(x=df['室'],y=df['价格'],data=df) 141 plt.show() 142 143 #分别查看1,2,3,4室下面积与价格之间的关系 144 fig,ax = plt.subplots(2,2) 145 ax1 = ax[0,0] 146 ax2 = ax[0,1] 147 ax3 = ax[1,0] 148 ax4 = ax[1,1] 149 axes = [ax1,ax2,ax3,ax4] 150 fig.set_size_inches(12,12) 151 dt = df[df['室']==1] 152 dt_1 = df[df['室']==2] 153 dt_2 = df[df['室']==3] 154 dt_3 = df[df['室']==4] 155 dts = [dt,dt_1,dt_2,dt_3] 156 for a,b in zip(dts,axes): 157 sns.scatterplot(x='面积',y='价格',data=a,ax=b) 158 plt.show() 159 print("价格数量TOP10是:\n",d_top10) 160 #分别查看室,卫,厅下面积与价格之间的关系 161 fig,ax = plt.subplots(2,2) 162 ax1 = ax[0,0] 163 ax2 = ax[0,1] 164 ax3 = ax[1,0] 165 axes = [ax1,ax2,ax3,] 166 fig.set_size_inches(12,12) 167 dt = df[df['室']==1] 168 dt_1 = df[df['卫']==2] 169 dt_2 = df[df['厅']==3] 170 dts = [dt,dt_1,dt_2,] 171 for a,b in zip(dts,axes): 172 sns.scatterplot(x='面积',y='价格',data=a,ax=b) 173 plt.show() 174 #查看各城市下待租房间数量 175 fig,ax = plt.subplots() 176 fig.set_size_inches(10,10) 177 xq = df['区'].value_counts().sort_values() 178 index = list(xq.index) 179 value = list(xq.values) 180 qx = pd.DataFrame({'区':index,'城市':value}) 181 qx.plot.barh(x='区',y='城市',ax=ax,color='blue',fontsize=12) 182 plt.legend(loc='right') 183 for a,b in zip(value,np.arange(0,14,1)): 184 plt.text(a+0.5,b,a,fontsize=12) 185 plt.show() 186 import numpy as np 187 import matplotlib.pyplot as plt 188 from mpl_toolkits. mplot3d import Axes3D 189 fig = plt.figure() 190 ax = Axes3D(fig,auto_add_to_figure=False) 191 fig.add_axes(ax) 192 x = df['价格'] 193 y = df['最近学校距离'] 194 z = df['周边学校个数'] 195 ax. scatter(x, y, z) 196 ax.set_xlabel('价格') 197 ax.set_ylabel( '最近学校距离') 198 ax.set_zlabel('周边学校个数') 199 plt.show() 200 import numpy as np 201 import matplotlib.pyplot as plt 202 from mpl_toolkits. mplot3d import Axes3D 203 fig = plt.figure() 204 ax = Axes3D(fig,auto_add_to_figure=False) 205 fig.add_axes(ax) 206 x = df['价格'] 207 y = df['最近医院距离'] 208 z = df['周边医院个数'] 209 ax. scatter(x, y, z) 210 ax.set_xlabel('价格') 211 ax.set_ylabel('最近医院距离') 212 ax.set_zlabel('周边医院个数') 213 plt.show()
四、总结
1.我对这次的大数据分析自感觉良好,完全符合自己的预期,虽然花费时间较长,但收获颇丰。通过对数据的分析和挖掘我发现当今社会人们对于卫生间的需求十分强烈,并且对周边地区有医院的地区反响比较良好。总的来说,当今住房问题仍然是一个需要重点关注的问题,需要政府进行宏观调控。
2.通过此次数据集分析练习,我熟练并掌握了数据归纳,数据清洗与可视化相关操作。这次的程序设计也是在对于python的知识进行一个熟悉的过程,通过本次任务,我对代码的掌握程度得到了一定程度上的提升,并且在完成任务的过程中提高了自己对于分析代码的能力,也是为以后正式编写代码程序打下坚实的基础。