Skip to content

Instantly share code, notes, and snippets.

@jehiah jehiah/pivot_csv.py
Created Nov 30, 2015

Embed
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):
columns.add(line[o.pivot_column])
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, ''))
w.writerow(row)
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)")
tornado.options.parse_command_line()
run()
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.