二手房房价数据分析与探索

0 数据说明

本项目使用的数据集来自于数据集,该数据集爬取的成都地区的二手房交易市场相关数据。 本文利用该数据集,通过相应数据分析了解成都各区域二手房房价、房源的基本情况,并对成都地区房价的影响因素进行探索。

1 理解数据

  • 首先通过pandas导入数据,将所有csv表格拼接好
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import os
%matplotlib inline

mpl.rcParams.update({
    'font.family': 'sans-serif',
    'font.sans-serif': ['Microsoft YaHei'],
    "font.size": 10
    })  # 设置全局字体

# 解决负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False

files = os.listdir('./data')
datas = []

for file in files:
    headname = './data/'
    filename = headname + file
    data = pd.read_csv(filename)
    datas.append(data)

house = pd.concat(datas)
house.head()

在这里插入图片描述
从上图中大致理解数据所包含的特征。 t i t l e title title是房主售卖的标题; p r i c e price price是房子的总价,单位是(万元); u n i t _ p r i c e unit\_price unit_price是单位房价,单位是(元/㎡); c o m m u n i t y _ n a m e community\_name community_name是社区的名称; r e g i o n region region是房子所在区域,包含区名和下属的地级市名称; t y p e type type是房子的类型,几室几厅几卫; c o n s t r u c t i o n _ a r e a construction\_area construction_area是房子的建筑面积; o r i e n t a t i o n orientation orientation是房子的朝向; d e c o r a t i o n decoration decoration是房子的装修情况; f l o o r floor floor是房子的楼层,描述房子所处楼层位置和所在楼的总楼层; e l e v a t o r elevator elevator是有无电梯的情况; p u r p o s e s purposes purposes是房子的用途; r e l e a s e _ d a t e release\_date release_date是房子发布时间; h o u s e _ s t r u c t u r e house\_structure house_structure是房子的结构;后面的 u r l s urls urls就是房子在网上的链接了。

  • 查看一下数据的整体情况
house.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 110556 entries, 0 to 1297
Data columns (total 16 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   title              110556 non-null  object 
 1   price              110556 non-null  float64
 2   unit_price         110556 non-null  float64
 3   community_name     110556 non-null  object 
 4   region             110556 non-null  object 
 5   type               110556 non-null  object 
 6   construction_area  110556 non-null  object 
 7   orientation        108777 non-null  object 
 8   decoration         108863 non-null  object 
 9   floor              110556 non-null  object 
 10  elevator           110556 non-null  object 
 11  purposes           110556 non-null  object 
 12  release_date       110556 non-null  object 
 13  house_structure    110412 non-null  object 
 14  image_urls         110556 non-null  object 
 15  from_url           110556 non-null  object 
dtypes: float64(2), object(14)
memory usage: 14.3+ MB

在这里可以看到数据有11万条左右,还是不少的。只有 p r i c e price price u n i t _ p r i c e unit\_price unit_price 是数值型特征,其余特征都不是。同时可以看到很多特征都是存在缺失值的,接下来需要对数据进行清洗。

2 数据清洗

接下来进行数据的清洗工作,主要包括去重、缺失值处理、文本清洗和异常值处理。

2.1 去重

house.duplicated().value_counts()

输出结果为:

False    103494
True       7062
dtype: int64

看到数据中有七千多行的数据是重复的。

  • 对数据进行去重
house.drop_duplicates(inplace=True)
house.index = range(len(house))

2.2 处理缺失值

house.isna().sum()

输出结果为:

title                   0
price                   0
unit_price              0
community_name          0
region                  0
type                    0
construction_area       0
orientation          1779
decoration           1693
floor                   0
elevator                0
purposes                0
release_date            0
house_structure       144
image_urls              0
from_url                0
dtype: int64
  1. 在这里可以看到 o r i e n t a t i o n orientation orientation d e c o r a t i o n decoration decoration有小两千的缺失值, h o u s e _ s t r u c t u r e house\_structure house_structure也有一百多个缺失值。
  2. 同时在之前观察数据的时候发现 t y p e type type f l o o r floor floor h o u s e _ s t r u c t u r e house\_structure house_structure等特征中很有多值为"暂无数据",其意义同缺失值是相同的。
  3. 通过对缺失数据所在行进行观察,发现缺失数据所在行的大部分特征信息都是比较完整的,只有零散的个别特征缺失。为了尽量保证更多的房源参与分析,确保分析结果的准确性,所以对缺失值处理如下:
  • 首先,通过对缺失数据所在行进行观察,发现有的房源 p u r p o s e s purposes purposes为车库,属于车位售出,不在二手房范畴内,所以首先将这部分数据删掉。
house.drop(house.loc[house.purposes=="车库"].index, axis=0, inplace=True)
house.index = range(len(house))

由于数据总共有11万行左右,某个特征缺失一小部分对于我们后续分析影响并不大。

  1. 对于包含"暂无数据"的特征,如果该特征是离散值的话,可以不作处理。因为之后要在 t y p e type type f l o o r floor floor这两个特征中提取连续性数值并转换为数值类型的特征,所以将其中包含的"暂无数据"处理为np.nan。
  2. 同样,对于包含缺失值的特征,如果该特征是离散值的话,处理为"暂无数据"。 t y p e type type f l o o r floor floor处理为np.nan。
  • 按照以上思路进行处理
house.loc[house.type=='暂无数据', 'type'] = np.nan
house.loc[house.floor=='暂无数据', 'floor'] = np.nan
house.loc[house.orientation.isna(), 'orientation'] = "暂无数据"
house.loc[house.decoration.isna(), 'decoration'] = "暂无数据"
house.loc[house.house_structure.isna(), 'house_structure'] = "暂无数据"
house.isna().sum()

输出结果为:

title                 0
price                 0
unit_price            0
community_name        0
region                0
type                 21
construction_area     0
orientation           0
decoration            0
floor                 7
elevator              0
purposes              0
release_date          0
house_structure       0
image_urls            0
from_url              0
dtype: int64

可以看到 t y p e type type f l o o r floor floor有一点缺失值(被填充为np.nan),其余的都被处理了。

  • 接下来看一下房源的发布年份
pd.to_datetime(house['release_date']).dt.to_period('A').value_counts()

输出结果为:

2019    74395
2018    13188
2020    11220
2017     1323
2015      743
2016      482
2012      471
2014      403
2010      346
2013      237
2009      133
2008       82
2011       78
2006       56
2007       50
2005       49
2000       38
2003       14
2002       12
2022        9
2001        8
2004        5
2021        5
1999        5
1998        4
1995        3
1988        2
1997        1
1996        1
Freq: A-DEC, Name: release_date, dtype: int64
  1. 发现有部分房源发布时间太老了,分析意义不大;
  2. 还有的房源的发布时间是2021年和2022年,时间超前了,这也是有问题的;
  3. 主要分析近年的情况,在这里只保留2010~2020年的数据,其余的数据先给删掉;
  4. 由于很多房源发布时间只有年份,没有具体的月份,所以取所有房源。
  • 按以上思路进行处理
ind = house.loc[~pd.to_datetime(house['release_date']).dt.to_period('A').astype(str).isin(['2010', '2011', '2012', '2013', '2014'
                                                                                           '2015', '2016', '2017', '2018', '2019', 
                                                                                           '2020'])].index
house.drop(ind, axis=0, inplace=True)
house.index = range(len(house))
# 取年份
house['release_date'] = pd.to_datetime(house.release_date).dt.to_period('Y')

2.3 文本数据清洗

在文本数据清洗上,主要有以下几点:

  1. c o n s t r u c t i o n _ a r e a construction\_area construction_area处理为数值类型, r e l e a s e _ d a t e release\_date release_date转换为统一的时间格式;

  2. r e g i o n region region t y p e type type f l o o r floor floor特征里的内容分割开来,从中提取新的数值特征;

  3. 除此之外,还要检查之后要分析的特征里的包含的值有没有异常;

  • 首先先处理 c o n s t r u c t i o n _ a r e a construction\_area construction_area,除去 m 2 m^2 m2,并将其转换为数值型格式
house['construction_area'] = house['construction_area'].str.replace('㎡', '')
house['construction_area'] = house['construction_area'].astype('float')
  • 接下来将 f l o o r floor floor划分为总楼层 t o t a l _ f l o o r total\_floor total_floor和房子所在楼层位置 f l o o r _ p o s i t i o n floor\_position floor_position两个特征,并加入到源数据中。因为总楼层和房子所在楼层位置都会对房子价格产生比较大的影响。
ins = house['floor'].str.split('(', expand=True)
house.insert(loc=9, column='floor_position', value=ins[0])
house.insert(loc=10, column='total_floor', value=ins[1])
house.floor_position = house.floor_position.str.replace(" ", "")
house.total_floor = house.total_floor.str.replace("共", "").str.replace(")", "").str.replace("层", "").str.replace(" ", "")
house.drop(['floor'], axis=1, inplace=True)
  • 接下来对 r e g i o n region region划分为区 d i s t r i c t district district和地级市 c o u n t y county county两个特征,并加入源数据中。
ins1 = house.region.str.split(",", expand=True)
ins1[0] = ins1[0].str.replace("'", "").str.replace("[", "").str.replace(" ", "")
ins1[1] = ins1[1].str.replace("'", "").str.replace("]", "").str.replace(" ", "")
house.insert(loc=4, column='district', value=ins1[0])
house.insert(loc=5, column='county', value=ins1[1])
house.drop(['region'], axis=1, inplace=True)
  • 接下来将 t y p e type type划分为 r o o m room room l i v i n g r o o m living room livingroom t o i l e t toilet toilet三个特征,并加入源数据中。
ins2 = house.type.str.split("室", expand=True)
ins3 = ins2[1].str.split("厅", expand=True)
ins3[1] = ins3[1].str.replace("卫", "")
house.insert(loc=6, column='room', value=ins2[0])
house.insert(loc=7, column='livingroom', value=ins3[0])
house.insert(loc=8, column='toilet', value=ins3[1])
  • 接下来除去 u r l url url特征
house.drop(['image_urls', 'from_url'], axis=1, inplace=True)
  • 检查一下 d i s t r i c t district district特征包含的值有没有异常
house.district.value_counts(dropna=False)

输出结果为:

成华        9921
双流        9793
青羊        8255
高新        7918
龙泉驿       7742
郫都        7726
温江        7521
金牛        6642
天府新区      6151
青白江       5196
武侯        4771
都江堰       3648
锦江        3295
新都        2955
高新西       2156
崇州        2011
简阳        1818
新津        1393
金堂         989
彭州         881
大邑         639
天府新区南区     215
蒲江          39
新都区         27
高新区         16
金堂]          9
青白江]         7
双流区          4
简阳]          2
Name: district, dtype: int64

看到有部分数据格式还是有点问题,需要继续处理一下。同时需要将相同的地区合并一下。

house.district = house.district.str.replace("]", "")

house.loc[house.district=='天府新区南区', 'district'] = '天府新区'
house.loc[house.district=='新都区', 'district'] = '新都'
house.loc[house.district=='高新区', 'district'] = '高新'
house.loc[house.district=='双流区', 'district'] = "双流"
  • r o o m room room l i v i n g r o o m living room livingroom t o i l e t toilet toilet特征转换为浮点数类型,去掉 t y p e type type
house.drop('type', axis=1, inplace=True)

#转换为float
house.room = house.room.astype("float")
house.livingroom = house.livingroom.astype("float")
house.toilet = house.toilet.astype("float")
  • 检查一下 o r i e n t a t i o n orientation orientation特征包含的值有没有异常
house.orientation.value_counts()

输出结果为:

东南           28839
南            26382
东            11818
西南            7943
北             4721
             ...  
南 东南 北           1
东南 南 西南 北        1
西南 北 东北          1
东南 西南 东北         1
东 西南 西           1
Name: orientation, Length: 117, dtype: int64

可以发现这里有很多零散的房子的 o r i e n t a t i o n orientation orientation非常长。可能是因为房主提供信息的时候把房子朝向和所有窗户朝向搞混了,如果填写了多个朝向的话,没有办法判断房子的朝向,所以归类到"暂无数据"当中。

house.loc[house.orientation.str.contains(" "), "orientation"] = "暂无数据"
house.orientation.value_counts(dropna=False)

输出结果为:

东南      28839
南       26382
东       11818
暂无数据    10178
西南       7943
北        4721
西        3878
西北       3430
东北       3200
南北       1317
东西         34
Name: orientation, dtype: int64

可以看到还有一些房屋朝向填写的"南北"、“东西”,这些也没有办法判断朝向,先不做处理了。

  • 检查一下 d e c o r a t i o n decoration decoration的值有没有异常
house.decoration.value_counts(dropna=False)

输出结果为:

精装      38621
简装      29837
毛坯      16484
其他      15030
暂无数据     1563
豪华装       142
简单装        63
Name: decoration, dtype: int64
  • 检查一下 f l o o r _ p o s i t i o n floor\_position floor_position特征包含的值有没有异常
house.floor_position.value_counts(dropna=False)

输出结果为:

高楼层    34211
中楼层    33601
低楼层    30447
中层       982
低层       680
高层       572
共3层      472
共1层      244
共2层      199
共4层      167
地下室       79
共5层       77
NaN        7
地下         2
Name: floor_position, dtype: int64

数据也同样有一些问题,需要将"地下"、“低层”、“中层”、“高层"分别合并到"地下室”、“低楼层”、“中楼层”、"高楼层"中。

house.loc[house.floor_position=='低层', 'floor_position'] = '低楼层'
house.loc[house.floor_position=='中层', 'floor_position'] = '中楼层'
house.loc[house.floor_position=='高层', 'floor_position'] = '高楼层'
house.loc[house.floor_position=='地下', 'floor_position'] = '地下室'
house.floor_position.value_counts(dropna=False)

输出结果为:

高楼层    34783
中楼层    34583
低楼层    31127
共3层      472
共1层      244
共2层      199
共4层      167
地下室       81
共5层       77
NaN        7
Name: floor_position, dtype: int64
  • 把nan转换为"暂无数据"当中去
house.loc[house.floor_position.isna(), 'floor_position'] = "暂无数据"
  • 如果 p u r p o s e s purposes purposes是"别墅",或者总楼层只有1~3层的话,那么是可以将其判定为"低楼层"的,先这样处理看看。
house.loc[house.floor_position.str.contains("共"), 'total_floor'] = house.loc[house.floor_position.str.contains("共"), 'floor_position'].str.replace("共", "").str.replace("层", "").str.replace(" ", "")
house.loc[(house.floor_position.str.contains("共")) & (house.purposes=='别墅'), 'floor_position'] = "低楼层"
house.loc[(house.floor_position.str.contains("共")) & (house.total_floor=='1'), 'floor_position'] = "低楼层"
house.loc[(house.floor_position.str.contains("共")) & (house.total_floor=='2'), 'floor_position'] = "低楼层"
house.loc[(house.floor_position.str.contains("共")) & (house.total_floor=='3'), 'floor_position'] = "低楼层"
house.floor_position.value_counts(dropna=False)

输出结果为:

高楼层     34783
中楼层     34583
低楼层     32103
共4层       113
地下室        81
共5层        70
暂无数据        7
Name: floor_position, dtype: int64
  • 通过这样处理,之前有问题的大部分都被处理好了。但是还有一些没法处理,也没办法判断楼层位置了,就处理为"暂无数据"。
house.loc[house.floor_position.str.contains("共4层"), 'floor_position'] = "暂无数据"
house.loc[house.floor_position.str.contains("共5层"), 'floor_position'] = "暂无数据"
  • 检查一下 t o t a l _ f l o o r total\_floor total_floor特征包含的值有没有异常
house.total_floor = house.total_floor.astype("float")
house.total_floor.value_counts(dropna=False)

输出结果没什么异常。

  • 检查一下 d e c o r a t i o n decoration decoration的值有没有异常
house.decoration.value_counts(dropna=False)

输出结果为:

精装      38621
简装      29837
毛坯      16484
其他      15030
暂无数据     1563
豪华装       142
简单装        63
Name: decoration, dtype: int64
  • 检查一下 e l e v a t o r elevator elevator特征包含的值有没有异常
house.elevator.value_counts()

输出结果为:

有       73768
无       27969
不满二年        2
满二年         1
Name: elevator, dtype: int64
  • 发现有五个不符合要求,查询这5个,发现其中4个总楼层在16层以上,所以这4个肯定是有电梯的,剩下一个总楼层为6层,大概率没有电梯,将这个设为"无"。
house.loc[(house.elevator.str.contains('满')) & (house.total_floor== 6), 'elevator'] = '无'
house.loc[(house.elevator.str.contains('满')), 'elevator'] = '有'
  • 检查一下 p u r p o s e s purposes purposes特征包含的值有没有异常
house.purposes.value_counts()

输出结果为:

普通住宅     89380
商业办公类     8532
别墅        3708
公寓         108
其他           6
平房           5
商住两用         1
Name: purposes, dtype: int64

其中,“其他”、“平房”、"商住两用"个数都比较少,先不管。

  • 检查一下 h o u s e _ s t r u c t u r e house\_structure house_structure特征包含的值有没有异常
house.house_structure.value_counts()

输出结果为:

钢混结构    67339
框架结构    16136
砖混结构    11697
未知结构     3912
混合结构     2164
钢结构       246
暂无数据      171
砖木结构       75
Name: house_structure, dtype: int64
  • 其中,"暂无数据"和"未知结构"是一个意思,合并在一起。
house.loc[house.house_structure=='暂无数据', 'house_structure'] = '未知结构'
house.house_structure.value_counts()

至此,特征文本清洗完成。

  • 接下来看一下描述性统计量
house.describe()

在这里插入图片描述

可以看到总房价、单位房价出现了0这种值,房子建筑面积也出现了832186这种特别大的值,这些都属于异常的数据,需要后续进行处理。

2.4 异常数据清洗

  • 先把建筑面积为0和832186的数据去掉
house.drop(house.construction_area.idxmax(), axis=0, inplace=True)
house.index = range(len(house))
house.drop(house[house.construction_area==0].index, axis=0, inplace=True)
house.index = range(len(house))
  • 去掉房价为0的数据
house.drop(house[house.price==0].index, axis=0, inplace=True)
  • 接下来通过箱线图找到成都各区域房价和房子建筑面积的异常点并去掉
plt.figure(figsize=(20, 10))
sns.boxplot(y="unit_price", x="district", data=house, )
plt.title("成都各区域的单位房价箱线图")    
plt.tight_layout()

在这里插入图片描述

可以看到锦江、金牛、都江堰、高新、高新西、龙泉驿、崇州、天府新区、新津的单位房价都有一些异常高的值,这些异常值可能是豪宅,本身就很非常贵,也有可能是房主提供信息有误,总之这对之后的区域总体房价分析会产生干扰,所以删掉这些异常值。

house.drop(house[(house['district']=='锦江') & (house['unit_price']>60000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='金牛') & (house['unit_price']>70000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='都江堰') & (house['unit_price']>40000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='高新') & (house['unit_price']>100000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='高新西') & (house['unit_price']>100000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='龙泉驿') & (house['unit_price']>80000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='崇州') & (house['unit_price']>32000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='天府新区') & (house['unit_price']>110000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='新津') & (house['unit_price']>40000)].index,inplace=True)
house.index = range(len(house))
  • 看一下成都各区域的建筑面积箱线图
plt.figure(figsize=(20, 10))
sns.boxplot(y="construction_area", x="district", data=house, )
plt.title("成都各区域的建筑面积箱线图")    
plt.tight_layout()

在这里插入图片描述

可以看到高新、金堂、崇州、天府新区、双流、蒲江、青羊、温江的房子建筑面积也有一些异常高的值,同样除掉这些异常值。

house.drop(house[(house['district']=='高新') & (house['construction_area']>1500)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='金堂') & (house['construction_area']>500)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='崇州') & (house['construction_area']>1000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='天府新区') & (house['construction_area']>1000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='双流') & (house['construction_area']>1000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='蒲江') & (house['construction_area']>400)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='青羊') & (house['construction_area']>1000)].index,inplace=True)
house.index = range(len(house))
house.drop(house[(house['district']=='温江') & (house['construction_area']>800)].index,inplace=True)
house.index = range(len(house))

2.5 数据描述性统计分析

  • 至此数据清洗工作大致完成,看一下描述性统计
house.describe()

在这里插入图片描述

成都二手房整体情况

  1. 清洗完毕后总共有 101711 101711 101711条数据;
  2. 每套房子平均建筑面积是 102 m 2 102m^2 102m2,房子建筑面积的中间 50 % 50\% 50% 72 m ∼ 120 m 2 72m^\sim120m^2 72m120m2之间,也就是说大部分的房型还是属于中小户型;
  3. 每套房子的平均单位房价为 1.47 万 / m 2 1.47万/m^2 1.47/m2,最高能达到 10.2 万 / m 2 10.2万/m^2 10.2/m2,房子平均单价的中间 50 % 50\% 50% 1.02 万 / m 2   1.78 万 / m 2 1.02万/m^2~1.78万/m^2 1.02/m2 1.78/m2左右。成都作为新一线城市的领头羊,这个房价的潜力还很大的;
  4. 每套房子的总价在 156 万 156万 156左右,房子总价的中间 50 % 50\% 50% 82 万 ∼ 178 万 82万\sim178万 82178
  5. 房子所在的总楼层平均值在 19 层 19层 19左右,房子总楼层的中间 50 % 50\% 50% 7 ∼ 29 7\sim29 729,说明大多数房源都来自小高层和高层;
  6. 可以看到二手房市场上的房子类型以 2 室 1 厅 1 卫 2室1厅1卫 211 2 室 2 厅 1 卫 2室2厅1卫 221 3 室 1 厅 1 卫 3室1厅1卫 311 3 室 2 厅 1 卫 3室2厅1卫 321 3 室 2 厅 2 卫 3室2厅2卫 322为主,这与市场上的主流房型也是一致的;
  7. 房价、单位房价、建筑面积的中位数都小于平均数,说明这三个特征的分布大概呈右侧长尾分布,是由少数偏大的值带高了平均值。

3 房价情况分析

3.1 成都整体房价情况分析

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
sns.distplot(house.unit_price, bins=50)
sns.despine()
plt.title("单位房价分布")
plt.ylabel("Frequency")

plt.subplot(1, 2, 2)
sns.distplot(house.price, bins=50)
sns.despine()
plt.title("总房价分布")
plt.ylabel("Frequency")
plt.tight_layout()

在这里插入图片描述

  1. 成都市二手房单位房价分布和总房价分布有明显的偏态,这与之前描述性统计的分析结果一致。
  2. 可以看到单位房价超过 4 万 / m 2 4万/m^2 4/m2的就已经非常少了,对于绝大部分购房者而言,都会去选择 0 ∼ 4 万 / m 2 0\sim4万/m^2 04/m2这个价位区间的房子。并且推测 4 万 / m 2 4万/m^2 4/m2是成都市很多区域二手房单位房价的上限,或者高于一些区域上限,这个会在后续分析中进行验证。

3.2 各区域的整体情况

3.2.1 各区域平均房价排名

plt.figure(figsize=(10, 8))
plt.subplot(2, 1, 1)
house.groupby("district").agg(np.mean).price.sort_values(ascending=False).plot(kind="bar", cmap='rainbow')
sns.despine()
plt.title('各区总房价排名')
plt.subplot(2, 1, 2)
house.groupby("district").agg(np.mean).unit_price.sort_values(ascending=False).plot(kind="bar", cmap='rainbow')
sns.despine()
plt.title('各区单位房价排名')
plt.tight_layout()

在这里插入图片描述

  1. 根据平均单位房价可划分为四个档次:

       2w以上 高新 锦江
       1.5w~2w 天府新区 青羊 武侯 成华 金牛
       1w~1.5w 双流 龙泉驿 高新西 温江 郫都 新都 新津
       1w以下 简阳 都江堰 ....
    
  2. 新津的平均单位房价排名靠后,但是平均总房价排名非常靠前,说明新津的别墅可能比较多,也有可能存在一些建筑面积很大的房子,这个会在后续分析中进行验证。

  3. 成华的情况和新津反过来了,说明成华区可能有很多小户型,同样在后续分析中进行验证。

3.2.2 各区域房价的分布

plt.figure(figsize=(20, 10))
plt.subplot(2, 1, 1)
sns.boxplot(y="unit_price", x="district", data=house)
plt.title("成都各区域的单位房价分布情况") 

plt.subplot(2, 1, 2)
sns.boxplot(y="price", x="district", data=house)
plt.title("成都各区域的总房价分布情况") 
plt.tight_layout()

在这里插入图片描述

从箱线图中发现

  1. 成都的老城区(锦江、成华、武侯、青羊区、金牛)因为地处成都市中心的原因,交通便利、配套设施完善,房价在整个成都排名前列。同时,从这几个区域单位房价箱线图上看也存在很多高于箱线上边缘的房价并且呈现出很长的连续性。这可能是由于这几个区经济发展水平较高,楼市开发也比较成熟,单位房价上才出现层次比较分明的现象。
  2. 在成都的新中心(高新、天府新区,双流,龙泉驿,新都,郫都,温江等)中,高新区和天府新区拥有得天独厚的优势,所以其房价和分布在老城区特点的表现上也更突出。除此之外,其余区域的房价也比较高了,处在第二梯队。
  3. 相比较而言,成都边缘城市房价比较低一些了。

3.2.3 各区域单位房价前两名的社区

house.groupby(['district', 'community_name']).mean().reset_index().groupby('district').apply(lambda x:x.sort_values('unit_price',ascending=False)[0:2])

在这里插入图片描述

  1. 从这个表中可以看到,成都市最贵的社区在天府新区,接近 10 万 / m 2 10万/m^2 10/m2。其次是高新区的 6.3 万 / m 2 6.3万/m^2 6.3/m2、青羊区的 5.4 万 / m 2 5.4万/m^2 5.4/m2
  2. 在3.1中分析成都总体平均单位房价分布的时候,发现超过 4 万 / m 2 4万/m^2 4/m2的房子比例很少,猜测 4 万 / m 2 4万/m^2 4/m2是很多区域房价上限。从这个表中可以看到在成都的 22 22 22个区域中,只有 4 4 4个区域的最贵社区超过了 4 万 / m 2 4万/m^2 4/m2(武侯区超了一点点,不计算在内),验证了 4 万 / m 2 4万/m^2 4/m2是很多区域房价上限的猜想。

3.2.4 成都市房价前20名社区

plt.figure(figsize=(10, 8))
plt.subplot(2, 1, 1)
house.groupby("community_name").agg(np.mean).price.sort_values(ascending=False)[0:20].plot(kind="bar", cmap='rainbow')
ylabel = 'price'
sns.despine()
plt.title('成都市单位房价前20名社区')

plt.subplot(2, 1, 2)
house.groupby("community_name").agg(np.mean).unit_price.sort_values(ascending=False)[0:20].plot(kind="bar", cmap='rainbow')
sns.despine()
plt.title('成都市总房价前20名社区')
ylabel = 'unit_price'
plt.tight_layout()

在这里插入图片描述

由上图可知,天府新区的麓山地区基本上占全了总房价和单位房价排名的前20名,看得出这个地区属于成都地区房价最高的地方。

4 房源分析

4.1 房源数量分析

4.1.1 各区域房源数量分析

plt.figure(figsize=(15, 8))
sns.countplot(x='district',data=house, order=house['district'].value_counts().sort_values(ascending=False).index)
plt.title('各区域房源数量')
sns.despine()

在这里插入图片描述

  1. 成华和双流的房源数量最多, 接近 10000 10000 10000套,其次青羊,超过了 8000 8000 8000套。接下来是高新、龙泉驿、郫都、温江、金牛、天府新区,都超过了 6000 6000 6000
  2. 简阳、新津、大邑、金堂、彭州较少,不足 2000 2000 2000套,蒲江最少,不到 100 100 100套。

4.1.2 不同房型的房源数量与房价分析

  • 看一下 X X X X X X X X X卫的房子的数量
house_type = house[['title', 'price', 'unit_price', 'room', 'livingroom', 'toilet', 'construction_area']].copy()
house_type['room_ins'] = house_type.room.map(str) + ','
house_type['livingroom_ins'] = house_type.livingroom.map(str) + ','
house_type['toilet_ins'] = house_type.toilet.map(str)
house_type['type'] = house_type.room_ins + house_type.livingroom_ins + house_type.toilet_ins
house_type.rename(columns={'title':'total'}, inplace=True)
sta = house_type.groupby('type').agg({"total": "count", "unit_price": np.mean, "price": np.mean, "construction_area": np.mean}).sort_values(by='total', ascending=False)

plt.figure(figsize=(8, 6))
sta['total'].iloc[0:10].plot(kind='bar')
plt.title('房源中各房型数量前10名')
plt.ylabel("数量/套")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  • 看一下前10名房型的平均单位房价和平均总房价
plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
plt.plot(sta['unit_price'].iloc[0:10])
plt.title('房源中各房型数量前10名的平均单位房价')
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()

plt.subplot(2, 1, 2)
sta['price'].iloc[0:10].plot(kind='bar')
plt.title('房源中各房型数量前10名的平均总房价')
plt.ylabel("平均总房价/万元")
sns.despine()
plt.tight_layout()

在这里插入图片描述

plt.figure(figsize=(8, 6))
sta['construction_area'].iloc[0:10].plot(kind='bar')
plt.title('房源中各房型数量前10名的平均建筑面积')
plt.ylabel("建筑面积/㎡")
sns.despine()
plt.tight_layout()
  • 看一下前10名房型的平均建筑面积
    在这里插入图片描述
  1. 可以看到市场上供应量最多的房型前三名是 3 室 2 厅 2 卫 3室2厅2卫 322 2 室 1 厅 1 卫 2室1厅1卫 211 2 室 2 厅 1 卫 2室2厅1卫 221,基本与市场上常见的房型也是一致的,其价格基本上是在 1.4 万 元 / m 2 1.4万元/m^2 1.4/m2左右,紧接着供应量最多的是 1 室 1 厅 1 卫 1室1厅1卫 111的小户型,这种户型对于经济实力稍差一些的家庭也是很好的选择,其价格也在 1.35 万 元 / m 2 1.35万元/m^2 1.35/m2
  2. 可以看到 3 室 1 厅 2 卫 3室1厅2卫 312的房子单位房价要比 3 室 2 厅 2 卫 3室2厅2卫 322要更贵, 3 室 1 厅 1 卫 3室1厅1卫 311的房子单位房价要比 3 室 2 厅 1 卫 3室2厅1卫 321要贵。从建筑面积上来讲, 3 室 1 厅 2 卫 3室1厅2卫 312 3 室 2 厅 2 卫 3室2厅2卫 322的建筑面积差不多, 3 室 1 厅 1 卫 3室1厅1卫 311 3 室 2 厅 1 卫 3室2厅1卫 321的建筑面积也差不多。2厅主要是饭厅和客厅,1厅则把饭厅客厅合并在一起,灵活性更好,空间更大,在市场上1厅更受欢迎一些,所以价格也更高。

4.2 各区域房源平均面积分析

house.groupby('district').agg(np.mean).construction_area.sort_values(ascending=False).plot(kind='bar')
plt.ylabel("面积/㎡")
sns.despine()
plt.title('各区域房源平均面积')

在这里插入图片描述
从图中看到,新津的房源平均建筑面积最大,成华的最少,这与3.2.1的推测也是吻合的。

  • 接下来详细验证一下,先看一下各区域别墅数量和房子建筑面积大于 700 m 2 700m^2 700m2的数量
plt.figure(figsize=(14, 4))
plt.subplot(1, 2, 1)
house[house.purposes=='别墅'].groupby('district').agg('count').title.sort_values(ascending=False).plot(kind='bar')
sns.despine()
plt.ylabel("房屋数量/套")
plt.title("各区域别墅数量")
plt.subplot(1, 2, 2)
house.query("construction_area>700").groupby('district').agg('count').title.sort_values(ascending=False).plot(kind='bar')
sns.despine()
plt.ylabel("房屋数量/套")
plt.title("各区域建筑面积超过700㎡房屋数量")
plt.tight_layout()

在这里插入图片描述
可以看到新津地区别墅数量不是最多的,但也算排在前列。其建筑面积超过 700 m 2 700m^2 700m2的房子数量是最多的。

  • 接下来看一下各区域小户型(建筑面积小于 70 m 2 70m^2 70m2)的数量
house.query("construction_area<70").groupby('district').agg('count').title.sort_values(ascending=False).plot(kind='bar')
plt.ylabel("房屋数量/套")
plt.title("各区域小户型数量")
sns.despine()

在这里插入图片描述
可以看到成华地区的小户型是最多的,超过了 3500 3500 3500套,这也验证了之前的推测。

5 单位的房价影响因素探索

5.1 房屋朝向对单位房价的影响

plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
house[house.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby('orientation').agg("count").title.plot(kind='bar')
plt.title("各类朝向房子的数量")
plt.ylabel("数量/套")
plt.subplot(2, 1, 2)
house[house.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby('orientation').agg(np.mean).unit_price.plot(kind='bar')
plt.title("各类朝向房子的平均单位房价")
plt.ylabel('单位房价(元/㎡)')
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 房子朝向以东南和南为主,这也也是最常见的朝向,因为朝南房子采光会好一些。
  2. 在价格上,房子朝向东南和南的反而最便宜。房子朝西、东北、北的房子最贵,超过了 1.6 万 / m 2 1.6万/m^2 1.6/m2。最贵的朝西和最便宜的朝东南差了 2000 元 2000元 2000左右。东南和南是最常见的朝向,所以各个区域都有很多这种朝向的房子,而房子朝西、东北、北比较少见,推测可能是由于这些房子在中心区域比较多。
  • 接下来算一下房子朝向在各个区域内的比例
plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
total_1 = house[house.orientation.isin(['西', '北', '东北'])].groupby("district").agg("count").title.values.sum()
dat = house[house.orientation.isin(['西', '北', '东北'])].groupby("district").agg("count").title / total_1
dat.sort_values(ascending=False).plot(kind='bar')
plt.ylabel('比例')
plt.title("朝向西、北、东北的房子数量在各个区域内的比例")
sns.despine()

plt.subplot(2, 1, 2)
total_2 = house[house.orientation.isin(['东南', '南'])].groupby("district").agg("count").title.values.sum()
dat_1 = house[house.orientation.isin(['东南', '南'])].groupby("district").agg("count").title / total_2
plt.ylabel('比例')
plt.title("朝向东南、南的房子数量在各个区域内的比例")
dat_1.sort_values(ascending=False).plot(kind='bar')
sns.despine()
plt.tight_layout()

在这里插入图片描述
从这里可以看出朝向 西 、 北 、 东 北 西、北、东北 西的房子聚集在新老中心区域的比例要明显大于朝向 西 、 北 、 东 北 西、北、东北 西的房子在新老中心区域的比例。这也解释了朝向 西 、 北 、 东 北 西、北、东北 西的房子价格为什么最高。

5.2 装修对房价的影响

由于"简单装"、“豪华装”、"暂无数据"的类别太少,首先将其排除后再进行统计。

plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
house[~house.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby('decoration').agg("count").title.plot(kind='bar')
plt.ylabel("数量/套")
plt.title("各类装修的房子数量")
plt.subplot(2, 1, 2)
house[~house.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby('decoration').agg(np.mean).unit_price.plot(kind='bar')
plt.ylabel("平均单位房价(元/㎡)")
plt.title("各类装修的平均单位房价")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 从数量上来看,精装>简装>毛坯。
  2. 从单位房价上来看,精装要比简装贵 2300 元 / m 2 2300元/m^2 2300/m2左右,简装和毛坯的单位房价基本差不多。

5.3 有无电梯对房价的影响

house.groupby('elevator').agg("count").title

输出结果为:

elevator
无    27952
有    73759
Name: title, dtype: int64
house.groupby('elevator').agg(np.mean).unit_price

输出结果为:

elevator
无    12450.296780
有    15509.547654
Name: unit_price, dtype: float64

可以看到接近 75 % 75\% 75%的房子都带电梯,并且带电梯房子比不带电梯房子单位房价要贵 3000 元 / m 2 3000元/m^2 3000/m2还多。这个也很好理解,因为有电梯对老人、年轻人上班族等上下楼比较方便,并且带有电梯的楼房建造要求也会高一些,建造成本更高。

5.4 住宅类型对房价的影响

由于"公寓"、“其他”、"商住两用"的类别太少,首先排除后再进行统计。

plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
house[~house.purposes.isin(['公寓', '其他', '商住两用', '平房'])].groupby('purposes').agg("count").title.plot(kind='bar')
plt.title("各住宅类型的房子数量")
plt.ylabel("数量/套")
plt.subplot(2, 1, 2)
house[~house.purposes.isin(['公寓', '其他', '商住两用', '平房'])].groupby('purposes').agg(np.mean).unit_price.plot(kind='bar')
plt.title("各住宅类型的平均单位房价")
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 市场上 85 % 85\% 85%房子都是普通住宅类;
  2. 别墅的平均单位房价比普通住宅贵 4000 元 / m 2 4000元/m^2 4000/m2左右,普通住宅的平均单位房价比商业办公类贵 2600 元 / m 2 2600元/m^2 2600/m2左右。

5.5 房屋结构对房价的影响

由于砖木结构、钢结构的类别太少,首先将其排除后再进行统计。

plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
house[~house.house_structure.isin(['砖木结构', '钢结构'])].groupby('house_structure').agg("count").title.plot(kind='bar')
plt.title("各类房屋结构的房子数量")
plt.ylabel("数量/套")

plt.subplot(2, 1, 2)
house[~house.house_structure.isin(['砖木结构', '钢结构'])].groupby('house_structure').agg(np.mean).unit_price.plot(kind='bar')
plt.title("各类房屋结构的平均单位房价")
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 市场上绝大部分房子都是钢混结构,其次是框架结构,还有小部分砖混结构。
  2. 钢混结构优点最突出,其建造成本也高,所以钢混结构的单位房价也高,其次是框架结构,砖混结构。钢混结构比框架结构高接近 2500 元 / m 2 2500元/m^2 2500/m2, 框架结构比砖混结构高 1000 元 / m 2 1000元/m^2 1000/m2还多。

5.6 房子所在楼层位置对房价的影响

由于"地下室", "暂无数据"样本量少,将其排除后再进行统计。

plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
house[~house.floor_position.isin(['地下室', '暂无数据'])].groupby('floor_position').agg("count").title.plot(kind='bar')
plt.title("房子所处楼层位置各类别的数量")
plt.ylabel("数量/套")
plt.subplot(2, 1, 2)
house[~house.floor_position.isin(['地下室', '暂无数据'])].groupby('floor_position').agg(np.mean).unit_price.plot(kind='bar')
plt.title("房子所处楼层位置的平均单位房价")
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 市场上低楼层数量稍微少一些,高、中楼层都比低楼层多 2000 2000 2000多套。
  2. 低楼层房价更高一些,然后是中楼层,最后是高楼层。
  3. 实际在高层楼盘中,次顶层是最贵的,高层比底层楼盘贵。由于数据有大量别墅,别墅都是低楼层,且其单位房价高,把低楼层价格也带高了。

5.7 发布时间对房价的关系

plt.figure(figsize=(8, 6))
plt.subplot(2, 1, 1)
house.groupby('release_date').agg("count").title.plot(kind='bar')
plt.title("不同年份房套子发布的数量")
plt.ylabel("数量/套")
plt.subplot(2, 1, 2)
house.groupby('release_date').agg(np.mean).unit_price.plot()
plt.title("不同年份的平均单位房价")
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 可以看到本次数据集的房源主要以 2019 年 2019年 2019发布的为主, 2018 年 2018年 2018以及 2020 年 1 ∼ 3 月 2020年1\sim3月 202013的房源也有 1 万 以 上 1万以上 1 2010 年 ∼ 2017 年 2010年\sim2017年 20102017的房源较少一些。
  2. 从单位房价上来看 2010 年 ∼ 2017 年 2010年\sim2017年 20102017单位房价一路上升, 2016 年 ∼ 2017 年 2016年\sim2017年 20162017的年增长最快,增幅接近 35 % 35\% 35% 2017 年 2017年 2017之后房价逐渐稳定下来,在小范围内波动, 2020 年 2020年 2020房价又有抬头趋势。虽然 2010 年 ∼ 2016 年 2010年\sim2016年 20102016的样本过少,但这样的房价趋势与我们的认知是一致的。

5.8 房价与楼层总数之间的关系

plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
house.groupby('total_floor').agg("count").title.plot(kind='bar')
plt.title("各楼层总数的房子数量")
plt.ylabel("数量/套")
plt.subplot(2, 1, 2)
house.groupby('total_floor').agg(np.mean).unit_price.plot()
plt.title("各楼层总数的房子平均单位房价")
plt.ylabel("平均单位房价(元/㎡)")
sns.despine()
plt.tight_layout()

在这里插入图片描述

  1. 从数量上来看,楼层总数为 6 层 6层 6的最多,有 1.4 万 1.4万 1.4套左右,其次楼层总数为 7 层 、 18 层 、 32 层 、 33 层 7层、18层、32层、33层 7183233数量也相对比较多
  2. 由于 1 层 、 2 层 和 35 层 往 上 1层、2层和35层往上 1235的数量比较少,所以平均单位房价波动很大。在楼层总数为 3 ∼ 34 层 3\sim34层 334中, 3 层 、 22 层 、 30 层 、 31 层 3层、22层、30层、31层 3223031价格最高。

5.9 对各个区域房价影响最大的因素

  • 首先针对某一个区域,计算其中一个特征所有类别的平均单位房价,之后取最大的和最小的计算极差。依次计算完所有特征的极差,极差最大的特征即该区域影响最大的因素。
house_new = house[['unit_price', 'district', 'orientation', 'decoration', 'floor_position', 'elevator', 'purposes', 'house_structure']]

def func(data):
    
    list_name = ['orientation_best', 'orientation_worst', 'orientation_diff', 
                 'decoration_best', 'decoration_worst', 'decoration_diff',
                 'floor_position_best', 'floor_position_worst', 'floor_position_diff',
                'elevator_best', 'elevator_worst', 'elevator_diff',
                 'purposes_best', 'purposes_worst', 'purposes_diff',
                 'structure_best', 'structure_worst', 'structure_diff', 'max_inf', 'max_diff']
    list_val = []
    max_diff = 0
    max_inf = ''
    
    ob1 = data.loc[data.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby("orientation").agg(np.mean)['unit_price'].idxmax()
    ow1 = data.loc[data.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby("orientation").agg(np.mean)['unit_price'].idxmin()
    best1 = data.loc[data.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby("orientation").agg(np.mean)['unit_price'].max()
    worst1 = data.loc[data.orientation.isin(['东', '南', '西', '北', '东南', '东北', '西南', '西北'])].groupby("orientation").agg(np.mean)['unit_price'].min()
    list_val.append(ob1)
    list_val.append(ow1)
    list_val.append(best1-worst1)
    max_diff = best1-worst1
    max_inf = 'orientation'

    ob2 = data.loc[~data.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby("decoration").agg(np.mean)['unit_price'].idxmax()
    ow2 = data.loc[~data.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby("decoration").agg(np.mean)['unit_price'].idxmin()
    best2 = data.loc[~data.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby("decoration").agg(np.mean)['unit_price'].max()
    worst2 = data.loc[~data.decoration.isin(['简单装', '豪华装', '暂无数据'])].groupby("decoration").agg(np.mean)['unit_price'].min()
    list_val.append(ob2)
    list_val.append(ow2)
    list_val.append(best2-worst2)
    if (best2-worst2)>max_diff:
        max_inf = 'decoration'
        max_diff = best2-worst2
    
    ob3 = data.loc[~house.floor_position.isin(['地下室', '暂无数据'])].groupby("floor_position").agg(np.mean)['unit_price'].idxmax()
    ow3 = data.loc[~house.floor_position.isin(['地下室', '暂无数据'])].groupby("floor_position").agg(np.mean)['unit_price'].idxmin()
    best3 = data.loc[~house.floor_position.isin(['地下室', '暂无数据'])].groupby("floor_position").agg(np.mean)['unit_price'].max()
    worst3 = data.loc[~house.floor_position.isin(['地下室', '暂无数据'])].groupby("floor_position").agg(np.mean)['unit_price'].min()
    list_val.append(ob1)
    list_val.append(ow1)
    list_val.append(best3-worst3)
    if (best3-worst3)>max_diff:
        max_inf = 'floor_position'
        max_diff = best3-worst3
    
    ob4 = data.groupby("elevator").agg(np.mean)['unit_price'].idxmax()
    ow4 = data.groupby("elevator").agg(np.mean)['unit_price'].idxmin()
    best4 = data.groupby("elevator").agg(np.mean)['unit_price'].max()
    worst4 = data.groupby("elevator").agg(np.mean)['unit_price'].min()
    list_val.append(ob4)
    list_val.append(ow4)
    list_val.append(best4-worst4)
    if (best4-worst4)>max_diff:
        max_inf = 'elevator'
        max_diff = best4-worst4
        
    ob5 = data.loc[~house.purposes.isin(['公寓', '其他', '商住两用', '平房', '商业办公类'])].groupby("purposes").agg(np.mean)['unit_price'].idxmax()
    ow5 = data.loc[~house.purposes.isin(['公寓', '其他', '商住两用', '平房', '商业办公类'])].groupby("purposes").agg(np.mean)['unit_price'].idxmin()
    best5 = data.loc[~house.purposes.isin(['公寓', '其他', '商住两用', '平房', '商业办公类'])].groupby("purposes").agg(np.mean)['unit_price'].max()
    worst5 = data.loc[~house.purposes.isin(['公寓', '其他', '商住两用', '平房', '商业办公类'])].groupby("purposes").agg(np.mean)['unit_price'].min()
    list_val.append(ob5)
    list_val.append(ow5)
    list_val.append(best5-worst5)  
    if (best5-worst5)>max_diff:
        max_inf = 'purposes'
        max_diff = best5-worst5
    
    ob6 = data.loc[~data.house_structure.isin(['砖木结构', '钢结构', '未知结构', '混合结构'])].groupby("house_structure").agg(np.mean)['unit_price'].idxmax()
    ow6 = data.loc[~data.house_structure.isin(['砖木结构', '钢结构', '未知结构', '混合结构'])].groupby("house_structure").agg(np.mean)['unit_price'].idxmin()
    best6 = data.loc[~data.house_structure.isin(['砖木结构', '钢结构', '未知结构', '混合结构'])].groupby("house_structure").agg(np.mean)['unit_price'].max()
    worst6 = data.loc[~data.house_structure.isin(['砖木结构', '钢结构', '未知结构', '混合结构'])].groupby("house_structure").agg(np.mean)['unit_price'].min()
    list_val.append(ob6)
    list_val.append(ow6)
    list_val.append(best6-worst6) 
    if (best6-worst6)>max_diff:
        max_inf = 'house_structure'
        max_diff = best6-worst6
    
    list_val.append(max_inf)
    list_val.append(max_diff)
    df = pd.DataFrame(np.array(list_val).reshape(1,-1), columns=list_name)
    
    return df

house_new.groupby("district").apply(func)

在这里插入图片描述

  1. 在成都市 22 22 22个区域中,有 15 15 15个区域影响因素最大的是房子用途 p u r p o s e s purposes purposes,有 3 3 3个区域影响因素最大的是房子结构 h o u s e _ s t r u c t u r e house\_structure house_structure,有1个是电梯 e l e v a t o r elevator elevator
  2. 同时,从表中也可以看到各区域中各个特征的影响力大小。
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值