Add extra column as the cumulative time difference

前端 未结 3 1311
滥情空心
滥情空心 2021-01-19 13:31

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         


        
3条回答
  •  抹茶落季
    2021-01-19 14:18

    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)
    

提交回复
热议问题