I have this Dataframe
lst = [['AA','Z',10,1,0],['BB','Y',10,1,0],
['AA','Z',20,2,0],['CC','X',10,2,0]]
df1 = pd.DataFrame(lst,columns = ['first_name','last_name','val','department','is_cum'])
looks like this
first_name last_name val department is_cum
0 AA Z 10 1 NO
1 BB Y 10 1 NO
2 AA Z 20 2 NO
3 CC X 10 2 NO
I want output something like this
first_name last_name val department is_cum
0 AA Z 10 1 NO
1 BB Y 10 1 NO
2 AA Z 10 1 YES
3 BB Y 10 1 YES
4 AA Z 20 2 NO
5 CC X 10 2 NO
6 AA Z 30 1,2 YES
7 CC X 10 2 YES
8 BB Y 10 1 YES
All the rows with is_cum NO is same as the input dataframe the newly populated rows are the cumulative rows with is_cum as YES.
Row 2 and 3 are the same as 0 and 1 as we have just one department to do cumulation. Row 6,7,8 is the cumulation of department 1 and department 2. If we have same first_name and last_name in department 1 and department 2 than add there val or else keep them as it is.
I was doing
df1.groupby(['first_name','last_name','department']).sum().groupby(level=0).cumsum()
after this, i could change the is_cum col and append these row in the original dataframe. But this is not the required output.