简体   繁体   中英

Python Pandas Groupby with 'Subtotal' values in output?

I am really new to python and was having some difficulty simplifying my steps to create a 'subtotal value' when using pandas groupby function on Source dataframe ['Product', 'Customer'].

Please help offer any pointers and solutions. Thank you!

# 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]
})

My own solution:

  1. Create an intermediate dataframe groupby 'Product' and aggregate 'Qty' with a column filled with 'Subtotal' string values
  2. Concatenate Source dataframe and Intermediate dataframe
  3. Perform groupby once again on ['Product', 'Customer'] to get Desired Output

It does not work if the Customer's name starts with a letter after 'S' because it may be sorted below 'Subtotal'.

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

I've run into this issue often with my workflow. One thing you can do that's kind of hacky, but works is use '[Subtotal]' instead of 'Subtotal'. The brackets around it will sort it correctly for you.

Here's where I've asked & been answered on similar questions before.

link 1

link 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM