Skip to content

Instantly share code, notes, and snippets.

@jehiah jehiah/
Created Nov 30, 2015

What would you like to do?
Pivot a CSV table.
#!/usr/bin/env python
import sys
import csv
import tornado.options
from collections import defaultdict
def run():
data = defaultdict(dict)
columns = set()
o = tornado.options.options
for line in csv.reader(sys.stdin):
data[line[o.row_column]][line[o.pivot_column]] = line[o.value_column]
columns = sorted(columns)
w = csv.writer(sys.stdout)
w.writerow(['key'] + columns)
for key, row_data in data.items():
row = [key]
for column in columns:
row.append(row_data.get(column, ''))
if __name__ == "__main__":
tornado.options.define("pivot_column", default=1, type=int, help="column to pivot on (starting at 0)")
tornado.options.define("row_column", default=0, type=int, help="key for rows (starting at 0)")
tornado.options.define("value_column", default=2, type=int, help="value for rows (starting at 0)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.