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

Dataframe筛选值解包报错:ValueError与UnboundLocalError求助

公寓定价预测程序错误分析与解决

错误1:ValueError: too many values to unpack (expected 2)

原因

find_district_info函数在无匹配数据时返回字符串"District not found",但调用时用semt, avg_price = ...尝试将单个字符串拆分为两个变量,类型不匹配导致报错。

错误2:UnboundLocalError: cannot access local variable 'semt'

原因

  1. 筛选条件错误:函数最后一个筛选条件错误地用material used and luxuriness列匹配prestige_of_that_district_and_its_vicinity参数,导致筛选逻辑失效,可能出现district_info非空但无法正确提取semt的情况;
  2. 变量定义未覆盖所有分支:若len(district_info)不为0,但后续提取semt的代码执行失败(比如列名错误),会导致semt未定义就执行return semt, avg_price。

修复方案

1. 统一函数返回值类型

避免混合返回字符串和元组,改为返回(None, None)表示无匹配,确保返回类型一致:

def find_district_info(district, BuildingAge,SquareMeter,Floor,Number_of_Floors,Elevator,number_of_bathrooms,Otopark,steeped_alley,material_used_and_luxuriness,prestige_of_that_district_and_its_vicinity):
    # 修复筛选条件:最后一行替换为prestige对应的正确列名(需与transactions_master_df实际列名一致)
    district_info = transactions_master_df[(transactions_master_df['District'] == district) & 
                                           (transactions_master_df['Building Age'] == BuildingAge)&
                                           (transactions_master_df['SquareMeter'] == SquareMeter)&
                                           (transactions_master_df['Floor'] == Floor)&
                                           (transactions_master_df['Number of Floors'] == Number_of_Floors)&
                                           (transactions_master_df['Elevator'] == Elevator)&
                                           (transactions_master_df['number of bathrooms'] == number_of_bathrooms)&
                                           (transactions_master_df['Otopark'] == Otopark)&
                                           (transactions_master_df['steeped alley'] == steeped_alley)&
                                           (transactions_master_df['material used and luxuriness'] == material_used_and_luxuriness)&
                                           # 替换为prestige字段的正确列名
                                           (transactions_master_df['prestige of that district and its vicinity'] == prestige_of_that_district_and_its_vicinity)
                                                                                                    ]
    
    if len(district_info) == 0:
        return None, None  # 统一返回元组类型
    
    semt = district_info.iloc[0]['Semt']
    avg_price = district_info['Price'].mean()
    
    return semt, avg_price

2. 调整调用逻辑

根据返回值的统一类型做判断,避免解包错误:

semt, avg_price = find_district_info(district_input, BuildingAge, SquareMeter, Floor, Number_of_Floors, Elevator, number_of_bathrooms, Otopark, steeped_alley, material_used_and_luxuriness, prestige_of_that_district_and_its_vicinity)
if semt is not None and avg_price is not None:
    print(f"For the district {district_input}:")
    print(f"Semt: {semt}")
    print(f"Average apartment price: {avg_price}")
else:
    print("No matching data found for the given parameters")

3. 额外优化建议

  • 核对transactions_master_df的列名,确保与筛选条件中的列名完全一致(注意大小写、空格);
  • 避免用==强匹配所有字段,可考虑放宽筛选条件(比如面积范围、楼龄范围),减少无匹配的情况;
  • 在函数开头初始化semt和avg_price为默认值,避免极端情况出现未定义错误:
def find_district_info(...):
    semt = None
    avg_price = None
    # 后续筛选逻辑...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 23:17:02