pandas练习题

导包+导入数据

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'))
相关推荐
默_笙10 小时前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_4260039610 小时前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫11 小时前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技11 小时前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读11 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
Asa1213811 小时前
R语言实现多组学因子分析(MOFA)
数据分析
只睡四小时12 小时前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊13 小时前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd12313 小时前
2026年第38周GitHub趋势周报
python·科技·github
漂着的圆木13 小时前
Agent 功能参与度:Copilot 怎么算
sql·数据分析·agent·githubcopilot·度量