首页 > 解决方案 > pivot_table 返回长度错误的表

问题描述

我对 pandas(python)的 pivot_table 函数的输出有疑问。我试图在 ProductName 上旋转一个表,将 CustomerId 设置为索引并聚合 Quantities,以获得每个客户和类别的总销售额。我使用的代码如下:

   products_sum = pd.pivot_table(sales_per_customer, 
        values='Quantity', 
        index='CustomerId', 
        columns='ProductName',
        aggfunc = "sum").reset_index()
   products_sum

问题是,虽然我有 107157 个唯一的 CustomerId,但 pivot_table 函数返回一个包含 106934 行的数据框(缺少 223 个 CustomerId)。我已经检查了 CustomerIds 上没有空值。

先感谢您

标签: pythonpandaspivot-table

解决方案


推荐阅读