I have a dataframe like:
Task ID Value Sno
A A1 5 1
A A1 2 2
B A1 4 3
A A1 1 4
A B1 10 5
C B1 3 6
D B1 5 7
D B1 2 8
D B1 12 9
E C1 25 10
And the expected output should look like
Task ID Value Sno
A A1 7 1,2
B A1 4 3
A A1 1 4
A B1 10 5
C B1 3 6
D B1 19 7,8,9
E C1 25 10
So, for each record where ID is same, the same consecutive 'Task' has to be merged and the 'Value' to be added. My approach is listed below but with that, it is not considering the cases where the same 'Task' occurs more than twice (the 'D' task in the example). Also, not able to drop the rows after merging, don't know why.
for j in range(len(df)-1):
if(df.loc[j,'Task']==df.loc[j+1, 'Task'] and df.loc[j,'ID']==df.loc[j+1, 'ID']):
df.loc[j, 'Value'] = df.loc[j, 'Value'] + df.loc[j+1, 'Value']
df.loc[j, 'ID'] = str(df.loc[j, 'ID']) + "," + str(df.loc[j+1, 'ID'])
df.drop([j+1])