背景知识要求
Python的pandas库。
pandas官网:http://pandas.pydata.org/
摘要
本文使用pandas库对上一篇文章:链家房源数据爬取(Scrapy)https://blog.csdn.net/qq_39206674/article/details/90114296 获取的数据进行清洗和预处理。
正文
数据清洗
原始数据部分内容如下:
数据清洗过程包括:
- 去除Nan数据和重复数据
- 去除未知和有缺失的数据
- 去除经过分析无用的数据(以房源为例:车位、别墅数据为无用数据)
数据清洗代码:
# 读文件
df = pd.read_csv(r'lianjia.csv', sep=',')
# 打印原始基本信息
print("original data rowCount: %d" % (df.shape[0]))
print("original data colCount: %d" % (df.shape[1]))
print(df.dtypes)
# 去除Nan数据和重复数据
df.dropna(inplace=True)
df.drop_duplicates(keep="first", inplace=True)
# 去除未知、暂无数据、车位、别墅
df = df[df["build_year"] != "未知"]
df = df[df["gross_area"] != "暂无数据"]
df = df[df["house_orientation"] != "暂无数据"]
df = df[df["usable_area"] != "暂无数据"]
df = df[df["with_elevator"] != "暂无数据"]
df = df[df["year_of_property"] != "未知"]
df = df[(df["household_style"]