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

如何从DataFrame取值作为Plotly Dash指标仪表盘的轴范围与数值参数

问题根因

你从DataFrame中提取数值时,实际拿到的是pandas.Series类型的序列对象,而非Plotly Indicator要求的单个int/float标量值,因此触发类型报错。

修复方案

标量值提取方法

替换你当前使用的.iloc、.values写法,选择以下任意一种方式提取单个数值:

  • 写法1:.iloc后加.item()直接转标量
# 假设df为筛选后的单客户对应单行数据
used_hours = df['已用工时'].iloc[0].item()
allowed_hours = df['允许工时'].iloc[0].item()
  • 写法2:用iat按位置直接取标量
used_hours = df.iat[0, df.columns.get_loc('已用工时')]
allowed_hours = df.iat[0, df.columns.get_loc('允许工时')]
  • 写法3:numpy数组转标量
used_hours = df['已用工时'].values[0].item()
allowed_hours = df['允许工时'].values[0].item()

Indicator仪表盘配置示例

拿到标量值后,按如下方式配置图表即可适配不同客户的允许工时范围:

import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
    mode = "gauge+number",
    value = used_hours,
    title = {"text": "工时使用进度"},
    gauge = {
        "axis": {"range": [0, allowed_hours]},
        "bar": {"color": "#1f77b4"}
    }
))

校验方法

如果仍报错,可在赋值后添加打印语句确认数值类型:

print(type(used_hours), used_hours)
# 预期输出示例:<class 'int'> 120 或 <class 'float'> 85.5

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 17:18:00