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

Plotly Dash问题:Dropdown输入筛选全局DataFrame返回空表

Plotly Dash多页面项目筛选全局DataFrame触发IndexError问题

问题背景

开发Plotly Dash多页面项目时,通过Dropdown筛选全局DataFrame(df_test)返回空表,触发IndexError,但相同逻辑在Jupyter Notebook可正常运行。

全局DataFrame加载代码

df_test = pd.read_parquet(PATH)
all_customers = df_test.customer_ID.values.tolist()
customer_IDs = random.choices(all_customers, k=5)

dcc.Dropdown(id='customer-dpdn-2',
             options = [{'label': x, 'value': x} for x in customer_IDs],
             className = 'mb-4'
             )

Graph组件代码

dcc.Graph(id='predictions-donut', figure={}, className='rounded')

回调函数代码

@callback(
    Output(component_id='predictions-donut', component_property='figure'),
    Input(component_id='customer-dpdn-2', component_property='value')
)
def donut_chart(customer):
    default_prob = df_test[df_test.customer_ID==customer].probability.values[0]
    no_default_prob = 1 - default_prob
    labels = ['Default', 'No default']
    values = [default_prob, no_default_prob]
    return go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])

报错信息

Traceback (most recent call last):
  File "C:\Users\***\page1.py", line 389, in donut_chart
    default_prob = df_test[df_test.customer_ID==customer].probability.values[0]
IndexError: index 0 is out of bounds for axis 0 with size 0

疑问

Dropdown选项提取自df_test的customer_ID列,为何用其筛选会得到空DataFrame?


问题原因及解决方案

可能原因

  • 多页面全局变量不一致:Dash多页面架构下,不同页面/模块可能各自加载df_test,导致回调使用的DataFrame和生成Dropdown选项的DataFrame不是同一个实例,数据不匹配。
  • Dropdown初始值为空:Dropdown未设置初始选中值时,回调接收的customer参数为None,此时筛选条件df_test.customer_ID==None返回空表。
  • 数据类型不匹配:customer_ID在DataFrame中的类型与Dropdown传递的value类型不一致(比如一个是字符串、一个是整数),导致匹配失败。

解决方案

  1. 统一全局数据加载
    将df_test放在独立模块中加载,所有页面和回调都从该模块导入,确保使用同一数据源:

    # data_loader.py
    import pandas as pd
    PATH = "你的文件路径.parquet"
    df_test = pd.read_parquet(PATH)
    

    在页面和回调文件中导入:from data_loader import df_test

  2. 处理空输入情况

    • 给Dropdown设置初始选中值,避免customer为None:
      dcc.Dropdown(id='customer-dpdn-2',
                   options = [{'label': x, 'value': x} for x in customer_IDs],
                   value=customer_IDs[0] if customer_IDs else None,
                   className = 'mb-4'
                   )
      
    • 在回调中增加空值和空表判断,避免索引错误:
      def donut_chart(customer):
          if not customer:
              return go.Figure()  # 返回空图表或提示性图表
          filtered_df = df_test[df_test.customer_ID == customer]
          if filtered_df.empty:
              return go.Figure(data=[go.Pie(labels=["无匹配数据"], values=[1])])
          default_prob = filtered_df.probability.values[0]
          no_default_prob = 1 - default_prob
          return go.Figure(data=[go.Pie(labels=['Default', 'No default'], 
                                        values=[default_prob, no_default_prob], 
                                        hole=.3)])
      
  3. 确保数据类型一致
    强制转换customer_ID为字符串类型,避免类型不匹配:

    all_customers = df_test.customer_ID.astype(str).values.tolist()
    customer_IDs = random.choices(all_customers, k=5)
    

内容的提问来源于stack exchange,提问作者Dušan Pavlović

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:40:49