How do i add a column of totals only if the "Name" variable is the same

63 Views Asked by At

enter image description here

I am trying to keep track of inventory and we use the Frebreeze for example,

There are 2 cases of 6 so 12 sub total.

Lets say we buy 2 of those, and there are two separate data point.

I want a column with 24 total.

I want to add the Total tab to make a Inventory tab based upon Name.

Another example

We buy two six packs of Diet Coke, I want the program to say if the df["UPC"] are the same, then add the "subTotal" column to make an "Total" Column with the accumulated Totals based upon name.

I want to be able to look at Diet and say "Ok we bought a six pack, but total we have 24 cans of soda "

Havent tried anything to be honest, I feel like theres a way to make a if and statement for this but I just can't put my finger on it

1

There are 1 best solutions below

2
On

As far as I understand your request, you need a summation based on UPC column.

If so:

df['Total_by_UPC'] = df.groupby('UPC').agg({'subTotal' : 'sum'})

Another option: cumulative sum of subTotal column based on UPC.

df['CumulativeSum_by_UPC'] = df.groupby('UPC').agg({'subTotal' : 'cumsum'})