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:
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.