Anton From Perm

Just another WordPress.com weblog

Posts Tagged ‘CSV

Easily unpivot data using Python

with one comment

Denormalized data in multiple columns

I was looking for an easy way to unpivot data, i.e. expand values from multiple columns in a single record into multiple records with the same values in a single column.

Normalized data in multiple records

Since the only tool available was SQL Server/SSIS, I wrote a short script in python (less than 50 lines of code) to easily unpivot CSV data.

Get the code here: http://pastie.textmate.org/713615

Options:

  • -v Verbose
  • -i Input file
  • -o Output file
  • -c Number of columns to be “frozen”, the default is one

Sample usage:
python unpivot.py -v -i GRP.csv -o GRPunpivot.csv -c 1

Written by antonfromperm

November 23, 2009 at 11:16 pm