Created
October 10, 2016 20:59
-
-
Save dogrdon/ac3d0286fc88efd0099709d4b3db3653 to your computer and use it in GitHub Desktop.
collapse csv rows by a column value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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