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

如何修改Python代码处理租金价格范围值并计算统一周租均价?

如何修改Python代码处理租金价格范围值并计算统一周租均价?

嘿,我完全懂你现在的困扰——原来的代码只能抓单个价格,碰到价格范围、多房源报价或者混合单位的情况就直接歇菜了对吧?别慌,咱们来一步步调整代码,把这些复杂情况都搞定。

核心思路调整

原来用re.search只会提取第一个匹配的价格,咱们换成re.findall把文本里所有价格数字都抓出来;然后把所有价格统一转换成周租单位,最后对多个价格(不管是范围还是多房源报价)计算平均值,这样就能得到统一格式的周租均价了。

修改后的完整代码

import re
import pandas as pd

data = {
'prices': [
'$350.00',
'$450.00 pw',
'$325 per week',
'$495pw - Views! White goods!',
'$460p/w + gst and outgoings',
'$300 wk',
'$390pw / $1695pcm',
'$180 pw / $782 pm',
'$375 Per Week/Fully Furnished',
'$350 pw + GST & Outgoings',
'APPLY NOW - From $270 per week',
'$185 per night',
'$400pw incl. power',
'$500 weekly',
'$600 per week pw',
'$850 per week (Fully furnished)',
'FROM $400PW, FURNITURE AND BILLS INCLUDED',
'THE DEAL- $780 PER WEEK',
'THE DEAL: $1,400 PER WEEK',
'$750/W Unfurnished',
'$320 - fully furnished pw',
'$330 PER WEEK | $1,430 P.C.M',
'Enquire Now: $690 per week',
'$460 per week / $1999 per month',
'$490 per week/Under Application approved',
'$1550pw - Location! Rare gem!',
'295 per week',  # Example without a dollar sign
'unit 2 - $780pw unit 3 - $760pw',  # Example with multiple prices
'$2500 pw high, $1600pw low,$380 pn',  # Example with multiple prices
'from $786 - $1572 per week',  # Example with multiple prices
'$590 to $639',  # Example with a range
'$280 - $290 pw'  # Example with a range
]
}

def extract_weekly_price(text):
    text = text.lower()
    # 提取所有数字部分(兼容带$、带逗号的千分位格式)
    price_strings = re.findall(r'\$?([\d,]+)', text)
    if not price_strings:
        return None
    
    # 把字符串转成数值,去掉千分位逗号
    raw_prices = []
    for s in price_strings:
        try:
            num = float(s.replace(',', ''))
            raw_prices.append(num)
        except ValueError:
            continue
    
    if not raw_prices:
        return None
    
    # 判断文本中的单位类型,用于统一转换为周租
    is_night = 'per night' in text or 'pn' in text
    is_month = any(kw in text for kw in ['pcm', 'p.c.m', 'pm', 'mth', 'per month'])
    is_year = 'p.a' in text or 'per annum' in text
    is_week = any(kw in text for kw in ['per week', 'pw', 'p/w', '/w', 'weekly', 'wk', 'per wk'])
    
    # 将所有价格统一转换为周租价格
    weekly_prices = []
    for price in raw_prices:
        if is_night:
            # 每晚价格转每周(乘以7)
            weekly = price * 7
        elif is_month:
            # 每月价格转每周(除以4.33,取平均每月周数)
            weekly = price / 4.33
        elif is_year:
            # 每年价格转每周(除以52)
            weekly = price / 52
        elif not is_week and not is_night and not is_month and not is_year:
            # 无明确单位时默认按周租处理(可根据你的数据集调整规则)
            weekly = price
        else:
            # 已经是周租相关单位,直接保留
            weekly = price
        weekly_prices.append(weekly)
    
    # 计算所有转换后价格的平均值
    avg_weekly = sum(weekly_prices) / len(weekly_prices)
    return round(avg_weekly, 3)

df = pd.DataFrame(data)
df['Weekly_rent'] = df['prices'].apply(extract_weekly_price)

# 查看处理结果
print(df[['prices', 'Weekly_rent']])

关键改进点说明

  1. 全量提取价格:用re.findall替代re.search,一次性抓取文本里所有价格数字,不管是单个价格、价格范围还是多房源报价。
  2. 兼容特殊格式:处理无$符号的价格(比如295 per week)、带千分位逗号的价格(比如$1,400 PER WEEK)。
  3. 智能单位转换:自动识别文本中的单位(每晚、每月、每年、每周),把所有价格统一转换成周租基准。
  4. 统一输出均价:对多个价格(范围或多房源)取平均值,保证输出格式一致。

特殊情况说明

如果碰到像$2500 pw high, $1600pw low,$380 pn这种混合单位的文本,当前逻辑会把所有价格都转换成周租后再平均(比如$380 pn会转成2660周租)。如果你希望只保留周租相关的价格,可以在代码里加个筛选逻辑——比如通过正则匹配价格附近的单位,只保留周租对应的价格再计算平均,不过这个需要更复杂的正则规则,如果你数据集里这种情况不多,当前逻辑应该完全够用。

备注:内容来源于stack exchange,提问作者X.x

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 13:28:16