How to add an extra column that is the cumulative value of the time differences for each course? For example, the initial table is:
id_A course we
import csv
import datetime as dt
with open('path/to/input') as fin, open('path/to/output', 'w') as fout:
infile = csv.DictReader(fin, delimiter='\t')
outfile = csv.DictWriter(fout, delimiter='\t', fieldnames=infile.fieldnames + ['cum_delta_sec'])
cdt = 0
last = None
for row in infile:
if last is None:
last = dt.strptime(row['ts_A'], "%Y-%m-%d %H:%M:%S")
row['cum_delta_sec'] = 0
outfile.writerow(row)
continue
cdt += (last - dt.strptime(row['ts_A'], "%Y-%m-%d %H:%M:%S")).total_seconds()
row['cum_delta_sec'] = cdt
outfile.writerow(row)