Pandas库分析处理链家出租房(实验)

xiaoxiao2021-02-28  6

载入数据

import pandas as pd lj_data = pd.read_csv('./LJdata.csv') lj_data.head(2)

规范一点,用英文的column name,这样免去了后续的一些问题(主要是编码问题)

lj_data.columns Index(['区域', '地址', '标题', '户型', '面积', '价格', '楼层', '建造时间', '朝向', '更新时间', '看房人数', '备注', '链接地址'], dtype='object') lj_data.columns = ['district', 'address', 'title', 'house_type', 'area', 'price', 'floor', 'build_time', 'direction', 'update_time', 'view_num', 'extra_info', 'link']

查看数据的形状和信息

lj_data.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 2760 entries, 0 to 2759 Data columns (total 13 columns): district 2760 non-null object address 2760 non-null object title 2760 non-null object house_type 2760 non-null object area 2760 non-null object price 2760 non-null int64 floor 2760 non-null object build_time 2758 non-null object ### direction 2760 non-null object update_time 2760 non-null object view_num 2760 non-null int64 extra_info 2760 non-null object link 2760 non-null object dtypes: int64(2), object(11) memory usage: 280.4+ KB lj_data.describe() price view_num count 2760.000000 2760.000000 mean 7570.800725 13.448913 std 6316.204986 12.746202 min 1300.000000 0.000000 25% 4500.000000 4.000000 50% 6000.000000 10.000000 75% 8500.000000 19.000000 max 210000.000000 122.000000 lj_data.shape (2760, 13)

找到最近更新信息的20套房子

lj_data.sort_values('update_time',ascending=False).head(20) #sort_values按参数列排序 #pd.to_datetime(lj_data['update_time'])

平均看房人数

int(sum(lj_data['view_num'])/len(lj_data['view_num'])) 13

房龄最小的20套房子的平均看房人数、平均面积…

import re import numpy as np def getnum(pattern,string): getnum=re.findall(pattern,string) if getnum: return getnum[0] return '' lj_data['hbt']=lj_data.fillna('不知道')['build_time'].apply(lambda x:getnum('\d+',x)) lj_data['ha']=lj_data['area'].apply(lambda x:getnum('\d+',x)) newest20=lj_data.sort_values('hbt',ascending=False).head(20) newest20['ha']=newest20['ha'].astype('int') 'newest20,mean visitor %s,mean size %s' % (sum(newest20['view_num'])/20,np.mean(newest20['ha'].values)) 'newest20,mean visitor 10.65,mean size 98.75'

房子价格的分布(平均,方差,中位数)

lj_data['price'].agg(['mean','std','median']) mean 7570.800725 std 6316.204986 median 6000.000000 Name: price, dtype: float64

最受欢迎的朝向(平均看房人数)

gbd=lj_data.groupby('direction') for k,v in gbd: kd=gbd.get_group(k) #获得k方向的数据项 mp=np.mean(kd['view_num']) print(k,mp) 东 13.7283950617 东 东北 34.0 东 东南 8.0 东 东南 南 21.0 东 北 19.4545454545 东 南 13.0285714286 东 南 北 12.9523809524 东 南 西 19.0 东 南 西 北 10.3333333333 东 西 14.0288461538 东 西 北 12.0 东 西北 16.3333333333 东 西南 12.0 东北 15.393442623 东南 14.1937172775 东南 东北 3.0 东南 北 10.0 东南 南 10.8 东南 西 3.66666666667 东南 西北 11.6666666667 东南 西南 14.6666666667 北 16.3693693694 南 13.6678899083 南 北 11.6798810704 南 西 16.4615384615 南 西 北 15.4444444444 南 西北 6.88888888889 南 西南 6.33333333333 西 13.5870967742 西 北 28.25 西北 19.4782608696 西南 16.4394904459 西南 东北 12.0 西南 北 7.63636363636 西南 西 25.0

房型数量分布

lj_data['house_type'].value_counts() #value_counts计算某一列各组出现频次 211284 11457 31371 32207 10157 22142 4242 4121 1217 209 1房间18 528 2房间16 535 514 3房间24 433 303 3房间13 333 622 2房间22 5房间21 631 Name: house_type, dtype: int64

最受欢迎的房型

lj2=lj_data[['house_type','view_num']] lj2.groupby('house_type').sum() view_num house_type 102242 117037 12290 1房间174 20142 2117589 221789 2房间1119 2房间232 3032 314467 322458 3312 3房间151 3房间257 41156 42398 4330 5121 5243 5332 5房间23 6224 6321

房子的平均租房价格(按平米算)

np.mean(lj_data['price']/lj_data['ha'].astype('float')) 87.722684299004541

最受关注的小区

ljadd=lj_data[['address','view_num']] ljaddsum=ljadd.groupby('address').sum() #各小区被访问总数 ljaddsort=ljaddsum.sort_values('view_num',ascending=False) #根据view_num列排序 ljaddsort.iloc[0,:] view_num 246 Name: 清芷园, dtype: int64

出租房源最多的小区

lj_data['address'].value_counts().head(1) 远洋山水 19 Name: address, dtype: int64

集中供暖和非集中供暖的有多少家,平均价格是多少

def count(col): i=0 for x in col: if '集中供暖' in x: i=i+1 return i count(lj_data['extra_info']) 2284 lj_data['ctlht']=lj_data['extra_info'].apply(lambda x:'集中供暖' in x) sum(lj_data['ctlht']==True) 2284

不同房型的平均/最大/最小面积

dha=pd.DataFrame({'house_type':lj_data['house_type'],'house_area':lj_data['ha'].astype('float')}) dha.groupby('house_type').agg(['mean','max','min']) house_area mean max min house_type 1048.484076 140.0 19.0 1154.669584 107.0 28.0 1272.411765 109.0 45.0 1房间142.875000 48.0 35.0 2049.888889 55.0 42.0 2178.725857 194.0 26.0 22106.028169 228.0 60.0 2房间160.166667 67.0 52.0 2房间237.000000 38.0 36.0 3067.000000 70.0 61.0 31114.237197 217.0 54.0 32145.690821 255.0 79.0 33144.000000 181.0 123.0 3房间185.333333 91.0 77.0 3房间246.750000 63.0 39.0 41165.285714 330.0 87.0 42196.333333 304.0 119.0 43179.000000 237.0 150.0 51138.250000 180.0 99.0 52242.125000 332.0 181.0 53191.600000 219.0 135.0 5房间2158.000000 158.0 158.0 62274.000000 316.0 232.0 63720.000000 720.0 720.0

哪个地铁口附近的房子最多

import re def findstation(reg,string,n): fst=re.search(reg,string) if fst: return fst.group(n) return '' lj_data['fst']=lj_data['extra_info'].apply(lambda x : findstation('距离(.+线)(\\(.*?段\\))?(.+站).*',x,n=3)) #reg分组,r修饰字符串 lj_data['fst'].value_counts() 1086 达官营站 43 双井站 34 安华桥站 32 青年路站 30 十里堡站 28 阜通站 28 北苑路北站 25 望京站 24 立水桥南站 22 东湖渠站 21 苏州街站 20 东直门站 19 朝阳公园站 19 广渠门外站 19 广渠门内站 19 安立路站 17 太阳宫站 17 劲松站 17 三元桥站 17 和平里北街站 16 大望路站 16 梨园站 16 惠新西街北口站 15 潘家园站 15 车公庄西站 15 芍药居站 15 昌平站 15 西直门站 14 车道沟站 14 ... 后沙峪站 2 金台夕照站 2 泥洼站 2 回龙观东大街站 2 农大南路站 1 枣营站 1 丰台科技园站 1 西北旺站 1 北海北站 1 肖村站 1 育知路站 1 北京站站 1 灯市口站 1 高碑店站 1 东单站 1 欢乐谷景区站 1 南邵站 1 国贸站 1 南楼梓庄站 1 广阳城站 1 郭公庄站 1 大钟寺站 1 呼家楼站 1 国家图书馆站 1 木樨地站 1 虎坊桥站 1 珠市口站 1 张自忠路站 1 复兴门站 1 天通苑南站 1 Name: fst, Length: 218, dtype: int64

地铁附近的房子平均价格 比 非地铁的高多少

#lj_data.drop(['hbt'],axis=1,inplace=True) #lj_data['fst'].ix[0,['fst']] lj_data['fstbool']=lj_data['fst']=='' a=lj_data[['price','fstbool']].groupby('fstbool').mean() a.values[0]-a.values[1] array([ 2414.36401381])

地铁附近的房源离地铁平均距离

#lj_data.drop(['fstbool'],axis=1,inplace=True) import re def findstation(reg,string,n): fst=re.search(reg,string) if fst: return fst.group(n) return 0 lj_data['subdis']=lj_data['extra_info'].apply(lambda x : findstation('距离(.+线)(\\(.*?段\\))?(.+站)(.+)米.*',x,n=4)) #reg分组,r修饰字符串 lj_data['subdis'].astype('float').mean() 435.20978260869566

把持不住了,写regular expression上瘾了。。。。

最多的在租楼层

#lj_data.drop('subdis',axis=1,inplace=True) import re def findfloor(reg,string,n): ff=re.search(reg,string) if ff: return ff.group(n) return '缺失楼层信息' lj_data['sortfloor']=lj_data['floor'].apply(lambda x:findfloor('(.楼层).*',x,1)) lj_data['sortfloor'].value_counts() 中楼层 1026 高楼层 932 低楼层 798 缺失楼层信息 4 Name: sortfloor, dtype: int64

随时看房的房子比例

lj_data['csuitv']=lj_data['extra_info'].transform(lambda x:'随时看房' in x) lj_data['csuitv'].mean() 0.99855072463768113
转载请注明原文地址: https://www.6miu.com/read-2000298.html

最新回复(0)