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

Python如何从pandas DataFrame中查询指定建筑类型对应的Probability 1值

实现方案

你可以选择以下两种常用方式实现查询需求:

  • 方案1:先将建筑类型列设为索引,后续查询效率更高,适合需要多次查询的场景
# 数据预处理步骤,仅需执行一次
df = df.set_index('Building')

# 查询示例:获取Office对应的Probability 1值
target_type = "Office"
prob1 = df.loc[target_type, "Probability 1"]

执行后prob1的取值就是0.49,符合预期。

  • 方案2:不修改原DataFrame结构,直接用布尔索引查询,适合单次查询场景
target_type = "Office"
prob1 = df.loc[df["Building"] == target_type, "Probability 1"].iloc[0]

跨脚本调用说明

如果需要在其他脚本中调用该数据,建议你先把DataFrame持久化存储为pickle格式,避免重复构造数据:

  1. 原数据脚本中执行存储:
df.to_pickle("building_prob.pkl")
  1. 其他需要调用的脚本中封装查询逻辑即可:
import pandas as pd

def query_building_prob1(building_type):
    df = pd.read_pickle("building_prob.pkl")
    # 增加容错判断,避免输入不存在的建筑类型时报错
    if building_type not in df["Building"].values:
        return None
    return df.loc[df["Building"] == building_type, "Probability 1"].iloc[0]

# 调用示例
print(query_building_prob1("Office"))

内容的提问来源于stack exchange,提问作者Karla Andrea Sandoval

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 22:21:00