 <p>If the groups you want to compress are all contiguous, then you're right that <code>itertools.groupby</code> could be useful. Assuming that (say) we want to preserve SEQ_TIME == 0 cases if they're the only member of a group or if there are three contiguous entries with a SEQ_TIME == 0, we could do something like (Python 3 csv open style):</p> <pre><code>import csv import itertools with open("dedup.csv", newline="") as fp_in, open("dedup_out.csv", "w", newline="") as fp_out: reader = csv.DictReader(fp_in) writer = csv.DictWriter(fp_out, reader.fieldnames) writer.writeheader() for key, group in itertools.groupby(reader, key=lambda row: row["ORIG"]): group = list(group) if len(group) == 2: group = [row for row in group if not float(row["SEQ_TIME"]) == 0] writer.writerows(group) </code></pre> <p>which gives me</p> <pre><code>TRAIN#,SEQ#,ORIG,DEP,DEST,ARR,SEQ_TIME A21,9,BPK,0.582986111,X66,0.584375,2 A21,10,X66,0.584375,CNLEMOYN,0.586805556,3.5 A21,11,CNLEMOYN,0.586805556,SMT,0.590972222,6 A21,13,SMT,0.590972222,CNCANAL,0.591666667,1 A21,14,CNCANAL,0.591666667,MEWILSPR,0.594791667,4.5 A21,16,MEWILSPR,0.594791667,MELEMONT,0.6,7.5 A21,17,MELEMONT,0.6,,0.6,6.5 A21,20,MELOCKPO,0.605208333,XUD,0.60625,2.5 A21,21,XUD,0.60625,JOL,0.607638889,2 </code></pre> <p>where the group conditions can be adjusted as you need. If you know there will never be any SEQ_TIME=0 cases you want to keep, the code could get even simpler, but this should give you a place to start.</p>