I'm using iterrows to loop through a dataframe and compare row n with row n+1. the algorithm is as follows:
if columns 0,1,2 of row_n != columns 0,1,2 of row_n+1
output row = row_n
then check row_n+1 vs row_n+2...
if columns 0,1,2 of row_n == columns 0,1,2 of row_n+1
output row columns 0,1,2,3 = row_n columns 0,1,2,3
output row column 4 = (row_n column 4 + row_n+1 column 4)
then "skip one row" and check row_n+2 vs row_n+3...
my current code works for the first comparison but then stops. My guess is that the problem is happening when Im trying to "skip one row". Im trying to use index = index+1 but the output does not look as expected. How can I fix this?
row_iterator = TSG_table_sorted.iterrows()
_, row_n1 = row_iterator.__next__()
for index, row_n0 in row_iterator:
Terminal_ID_n0 = row_n0['Terminal_ID'];
TSG_n0 = row_n0['TSG'];
Date_n0 = row_n0['Date'];
Vol_n0 = row_n0['Vol'];
Terminal_no_n0 = row_n0['Terminal_no'];
Terminal_ID_n1 = row_n1['Terminal_ID'];
TSG_n1 = row_n1['TSG'];
Date_n1 = row_n1['Date'];
Vol_n1 = row_n1['Vol'];
if (Terminal_ID_n0==Terminal_ID_n1 and TSG_n0==TSG_n1 and Date_n0==Date_n1):
new_vol=Vol_n0+Vol_n1;
output_table.loc[i]=[Terminal_ID_n0,TSG_n0,Date_n0,Terminal_no_n0,new_vol]
i=i+1;
else:
output_table.loc[i]=[Terminal_ID_n0,TSG_n0,Date_n0,Terminal_no_n0,Vol_n0]
i=i+1;
index=index+1;
input
Terminal_ID TSG Date Terminal_no Vol
508 t_tel_003 CashCheck 10/1/2018 003 61
9605 t_tel_003 CashCheck 10/1/2018 003 3
2309 t_tel_003 CommercialDeposit 10/1/2018 003 12
4439 t_tel_003 CommercialDeposit 10/1/2018 003 10
9513 t_tel_003 CommercialDeposit 10/1/2018 003 122
12282 t_tel_003 CommercialDeposit 10/1/2018 003 1
current output
Terminal_ID TSG Date Terminal_no Vol
0 t_tel_003 CashCheck 10/1/2018 003 64
1 t_tel_003 CommercialDeposit 10/1/2018 003 12
2 t_tel_003 CommercialDeposit 10/1/2018 003 10
3 t_tel_003 CommercialDeposit 10/1/2018 003 122
4 t_tel_003 CommercialDeposit 10/1/2018 003 1
expected output
Terminal_ID TSG Date Terminal_no Vol
0 t_tel_003 CashCheck 10/1/2018 003 64
1 t_tel_003 CommercialDeposit 10/1/2018 003 22
3 t_tel_003 CommercialDeposit 10/1/2018 003 123