将info2函数传入Dash回调时触发DataTable类型错误求协助
问题解决:Dash DataTable 数据类型不匹配错误
错误原因
错误提示明确指出data[7].refer的类型不符合要求,问题出在自定义函数info2的返回值:
- 当
row['header'] == 'Settled Date'时,你返回的是info['Customer ID'].unique(),这是一个数组/列表类型 - Dash DataTable要求每个字段的值必须是
string、number或boolean,数组类型不被接受
修复方案
把数组转换成字符串格式,根据你的需求选择以下两种方式:
方式1:将所有唯一Customer ID用逗号拼接成字符串
修改info2函数中对应分支的返回值:
def info2(row): if row['header'] == 'Settled Date': # 将数组转为逗号分隔的字符串 return ', '.join(map(str, info['Customer ID'].unique())) elif row['header'] == 'Rejected Date': return 'go to the next page' return ''
方式2:如果确定唯一Customer ID只有一个,直接取第一个元素
def info2(row): if row['header'] == 'Settled Date': # 取唯一值的第一个元素,转为字符串 unique_ids = info['Customer ID'].unique() return str(unique_ids[0]) if len(unique_ids) > 0 else '' elif row['header'] == 'Rejected Date': return 'go to the next page' return ''
验证修复
修改后,new_info['refer']列的所有值都会是字符串类型,符合DataTable的要求,错误即可消除。
内容的提问来源于stack exchange,提问作者beginofwork
相关产品推荐
相关产品推荐

