Data:
Date column1 column2 column3 column4
2021-08-20 19 30 11 8
2021-08-21 15 25 11 4
2021-08-22 5 10 5 0
2021-08-23 25 36 16 9
2021-08-24 6 6 6 0
I want to iterate 2 rows at a time and create a new column it is like backlog from previous day+today : In every second row of each iteration I want a value like: df['new_column'] = (df['column2']-df['column4']) from row 2 + (df['column2']-df['column4']) from row1
I am trying this:
from itertools import tee
from itertools import zip_longest as izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
for (idx1, row1), (idx2, row2) in pairwise(df.iterrows()):
print(idx1,row1,"\n\n",idx2,row2,"\n\n")
df['Backlog_today'][row2] = (df.loc[row2, ['column2']] - df.loc[row2, ['column4']])
df['Backlog_yesterday'][row1] = (df.loc[row1, ['column2']] - df.loc[row1, ['column4']])
df['new_column'] = df['Backlog_today'] + df['Backlog_yesterday']
How can I correct this?