导包+导入数据
python
import pandas as pd
house_df = pd.read_csv('E:/数据分析材料/LJdata.csv')
# 将列名修改成英文
house_df.columns = ['district', 'address', 'title', 'house_type', 'area', 'price', 'floor', 'build_time', 'direction', 'update_time', 'view_num', 'extra_info', 'link']
准备动作
1. 查看数据前5行
python
print(house_df.head())
2. 查看列数据分布
python
house_df.info()
3. 查看列统计指标
python
# 默认只统计数值型数据;include='all'统计所有类型的数据
print(house_df.describe(include='all'))
4. 查看数据形状
python
print(house_df.shape) # (2760,13)
具体的需求
1. 找到租金最低,和租金最高的房子
python
# 思路1:排序+head()
print(house_df.sort_values('price').head(1)) # 租金最低
print(house_df.sort_values('price').tail(1)) # 租金最高
# 思路2:nsmallest(), nlargest() 求最小/大的n个值
print(house_df.nsmallest(1,'price')) # 租金最低
print(house_df.nlargest(1,'price')) # 租金最高
# 思路3:筛选
print(house_df[house_df.price == house_df.price.min()]) # 租金最低
print(house_df[house_df.price == house_df.price.max()]) # 租金最高
2. 找到最近新上的10套房源
python
print(house_df.sort_values('update_time').tail(10))
3. 查看所有更新时间
python
print(house_df.update_time.unique())
4. 查看看房人数的平均值, 最大值, 最小值
python
print(house_df.view_num.mean())
print(house_df.view_num.max())
print(house_df.view_num.min())
5. 查看不同看房人数的房源数量,as_index = False 分组字段不作为行索引(默认为True)
python
print(house_df.groupby('view_num',as_index=False).agg({'district':'count'}))
6. 查看房租价格的分布, 例如: 平均值, 标准差, 中位数...
python
print(house_df.price.describe())
7. 找到看房人数最多的朝向
python
tem = house_df.groupby('direction',as_index=False).agg({'view_num':'count'})
print(tem.columns)
tem.columns=['direction', 'count']
print(tem.nlargest(1,'count'))
8. 查找最受欢迎的房型
python
tem = house_df.groupby('house_type',as_index=False).agg({'view_num':'count'})
print(tem.columns)
tem.columns=['house_type', 'count']
print(tem.nlargest(1,'count'))
9. 查找房子的平均租房价格 (元/平米)
python
# 先算每个房子的平均租房价格,再算全部的平均价格之和除以总数
house_df['price_avg'] = house_df.price/house_df.area
print(house_df.price_avg.mean())
10. 找到出租房源最多的小区
python
temp = house_df.groupby('address',as_index=False).agg({'area':'count'})
temp.columns = ['address','count']
print(temp.nlargest(1,'count'))