Anton From Perm

Just another WordPress.com weblog

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

Advertisements

Written by antonfromperm

November 23, 2009 at 11:16 pm

One Response

Subscribe to comments with RSS.

  1. Brilliant !!!

    Rajesh

    October 23, 2014 at 4:29 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: