You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas提取巴塞罗那房租数据中的月度租金价格

筛选巴塞罗那月度租金数据的方法

你可以通过布尔索引,基于Average_rent列的标识文本筛选出仅包含月度租金的行,具体操作如下:

实现步骤

  • 针对Average_rent列,匹配标识为average rent (euro/month)的记录
  • 从筛选后的子数据集中提取目标Price数据,或直接计算月度租金的统计值

修改后的代码

import pandas as pd
import numpy as np

# 读取数据集
data = pd.read_csv('Barcelona_rent_price.csv')

# 筛选月度租金数据:注意列名如果是带空格的'Average _rent',请替换为对应名称
monthly_rent_data = data[data['Average_rent'] == 'average rent (euro/month)']

# 输出仅月度租金的Price列
print(monthly_rent_data['Price'])

# 计算月度租金的平均值
print(np.mean(monthly_rent_data['Price']))

补充优化

如果数据里的标识文本存在细微格式差异,可改用字符串包含匹配提升兼容性:

# 匹配包含"euro/month"的所有记录
monthly_rent_data = data[data['Average_rent'].str.contains('euro/month')]

内容的提问来源于stack exchange,提问作者serhatdogan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:35:22