Python Pandas Groupby 输出中有 'Subtotal' 个值?

Python Pandas Groupby with 'Subtotal' values in output?

我是 python 的新手,在源数据帧 ['Product' 上使用 pandas groupby 函数时,在简化创建 'subtotal value' 的步骤时遇到了一些困难, 'Customer']。

请帮忙提供任何指示和解决方案。谢谢!

# Desired Output
Product Customer    Qty
Item A  Cust1       5
        Cust4       10
        Subtotal    15
Item B  Cust1       15
        Cust2       5
        Cust6       1
        Subtotal    21
Item C  Cust3       1
        Subtotal    1

# Source Dataframe
    Product Customer  Qty
0   Item A  Cust1     5
1   Item A  Cust4     10
2   Item B  Cust1     15
3   Item B  Cust2     5
4   Item B  Cust6     1
5   Item C  Cust3     1

# Source Dataframe code
source_df = pd.DataFrame({
    'Product'  : ['Item A', 'Item A', 'Item B', 'Item B','Item B', 'Item C'],
    'Customer' : ['Cust1', 'Cust4', 'Cust1', 'Cust2', 'Cust6', 'Cust3'],
    'Qty'      : [5,10,15,5,1,1]
})

我自己的解决方案:

  1. 根据 'Product' 创建一个中间数据框组,并聚合 'Qty' 与填充有 'Subtotal' 字符串值的列
  2. 连接源数据帧和中间数据帧
  3. 在 ['Product'、'Customer'] 上再次执行 groupby 以获得所需的输出

如果客户名称以 'S' 之后的字母开头,则无效,因为它可能排在 'Subtotal' 下面。

# Intermediate Subtotal Dataframe
    Product Customer    Qty
0   Item A  Subtotal    15
1   Item B  Subtotal    21
2   Item C  Subtotal    1

我 运行 在我的工作流程中经常遇到这个问题。您可以做的一件事有点老套,但有效的是使用“[小计]”而不是 'Subtotal'。它周围的括号会为您正确排序。

这是我之前就类似问题提出的问题和得到的回答。