根据上篇爬取的巴乐兔租房信息,来做一些相关的数据分析。
首先导入相应的库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import re
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
读取数据:
data_1 = pd.read_csv(r"./巴乐兔_上海.csv",sep="::",engine='python',encoding='utf-8',
names=["id","name","area","community","url","price","rent_type",
"size","traffic_info","release_time","grade","comment_num"])
data_1.head()
查看各个区域的房源个数:
data_1['area'].value_counts()
这里我发现数据有些异常,图中标红部分,因此我来查看一下这部分数据:
data_1[data_1['area'] == "56"]
由结果可以看到,实际区域是浦东,数据出错是由于之前爬虫做正则匹配时候,“-”匹配的时候匹配到“7-56号”了,既然原因找到了,正确的数据也找到了,那我们就可以对异常数据进行处理了,将区域显示的56改为浦东即可。
同样的,查看区域为29的数据可以发现:实际都是松江区域的租房信息。因此也同样的数据处理
data_1[data_1['area'] == "29"]
数据处理如下:
# 异常值处理:56代表的是浦东,29代表的是松江
data_1.loc[data_1['area'] == "56","area"] = "浦东"
data_1.loc[data_1['area'] == "29","area"] = "松江"
再来查看各个区域房源数量:
data_1['area'].