【问题标题】:ValueError when rendering html report using Pandas_profiling使用 Pandas_profiling 呈现 html 报告时出现 ValueError
【发布时间】:2026-01-24 22:50:01
【问题描述】:

为我的 DataFrame 的子集运行配置文件报告时出现以下错误。

ValueError: Value '6.180529706513958' should be a ratio between 1 and 0.

这行得通:

profile = ProfileReport(
    df, title="Profile Report of the January Conversion Dataset"
)
profile.to_file(Path("../../../products/jan_cvr_report.html"))

profile0 = ProfileReport(
    df[df['conversion']==0], title="Profile Report of the January Conversion==0 Dataset"
)
profile0.to_file(Path("../../../products/jan_cvr0_report.html"))

这不是:

profile1 = ProfileReport(
    df[df['conversion']==1], title="Profile Report of the January Conversion==1 Dataset"
)
profile1.to_file(Path("../../../products/jan_cvr1_report.html"))

【问题讨论】:

    标签: python pandas pandas-profiling


    【解决方案1】:

    我找到了一个closed Github issue,它建议我开始工作。我的详细信息和堆栈跟踪就在那里。

    解决方案:remove_unused_categories

    df1 = df[df['conversion']==1].copy(deep=True)
    df1.user_id.cat.remove_unused_categories(inplace=True)
    

    运行上述操作后,配置文件报告工作正常。这些类非常不平衡,因此当子集到 conversion=1 的大部分 user_ids 不使用时。这也可以通过不将user_id 作为一个类别来解决。但是,这可能是其他类别的问题,所以我还是要分享。

    【讨论】:

    • 所以,这个错误似乎是一种有用的方式,可以告诉您如何通过分组(例如使用自定义转换器)使类分布更加平衡。就我而言,我有一个基数为 9 的就业状态特征,将其减少为“已就业”、“失业”和“退休”就可以了(尽管您会丢失信息)。
    • 更新:再次遇到它,并通过为分类数据设置duplicates=None 得到修复,如下所述:pandas-profiling.github.io/pandas-profiling/docs/master/rtd/… - 有意义。
    最近更新 更多