Skip to content

Instantly share code, notes, and snippets.

@dogrdon
Created October 10, 2016 20:59
Show Gist options
  • Save dogrdon/ac3d0286fc88efd0099709d4b3db3653 to your computer and use it in GitHub Desktop.
Save dogrdon/ac3d0286fc88efd0099709d4b3db3653 to your computer and use it in GitHub Desktop.
collapse csv rows by a column value
'''you have a csv file with something like:
col1, col2
x, 1
x, 2
y, 1
z, 4
z, 8
and you want to output something like:
col1, col2
x, 1;2
y, 1
z, 4;8
Use itertools groupby
'''
import csv
from itertools import groupby
ifile = './path_to_csv.csv'
ofile = './path_to_output.csv'
with open(ifile, 'r') as i:
with open(ofile, 'w') as o:
reader = csv.reader(i)
writer = csv.writer(o)
header = next(reader)
writer.writerow(['col1', 'col2'])
grouped = groupby(reader, lambda x: x[0])
for key, group in grouped:
vals = list(group)
row = [key, ';'.join([v[2] for v in vals])]
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment