北上广深租房信息分析
分析目的
根据链家网北上广深四个城市的所有租房数据(时间节点:2019年2月25日),数据共有105258条。
分析不同地区,租房的高低主要与哪些因素有关
不同的因素导致的房价差异大概是多少
数据读取
# 导入模块
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
%matplotlib inline
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
df=pd.read_csv(r'D:\BI study download\北上广深租房\data_sample.csv')
df.head()
空值查询
df.isnull().sum()
这些空值对数据影响不大,因此不用处理
数据清洗
# 删除不需要的列
df= df.drop(columns='_id')
df = df.drop(columns='rent_price_unit')
# rent_area字段有些填写的是一个范围,比如23-25平房米,后期转换成“float”类型的时候不好转换,考虑取平均值
def get_aver(data):
if isinstance(data, str) and '-' in data:
low, high = data.split('-')
return (int(low)+int(high))/2
else:
return int(data)
df['rent_area'] = df['rent_area'].apply(get_aver)
# 价格是有区间的,需要按照处理rent_area一样的方法处理
df['rent_price_listing'] = df['rent_price_listing'].apply(get_aver)
df.info()
价格描述
df.rent_price_listing.hist(bins=50)
大部分价格处于300-15000之间,因此删除价格大于15000的数据,使其接近正态分布。
df=df[df['rent_price_listing']<=15000]
数据分析与可视化
各城市房源数量分布
def get_city_zf_loc(city, city_short, col=['longitude', 'latitude', 'dist'], data=df):
file_name = 'data_' + city_short + '_latlon.csv'
data_latlon = data.loc[data['city']==city, col].dropna(subset=['latitude',