{ "metadata": { "name": "", "signature": "sha256:f95cbe0c06a0f42dbf56fc216690f98a24781c36fd988c3bf5641e7280d062af" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Transpose A Dataframe In Pandas\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Import Preliminaries\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a dataset with the index being a set of names\n", "raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], \n", " 'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], \n", " 'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], \n", " 'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],\n", " 'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}\n", "df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'], index=df.name)\n", "df" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regimentcompanynamepreTestScorepostTestScore
name
Miller Nighthawks 1st Miller 4 25
Jacobson Nighthawks 1st Jacobson 24 94
Ali Nighthawks 2nd Ali 31 57
Milner Nighthawks 2nd Milner 2 62
Cooze Dragoons 1st Cooze 3 70
Jacon Dragoons 1st Jacon 4 25
Ryaner Dragoons 2nd Ryaner 24 94
Sone Dragoons 2nd Sone 31 57
Sloan Scouts 1st Sloan 2 62
Piger Scouts 1st Piger 3 70
Riani Scouts 2nd Riani 2 62
Ali Scouts 2nd Ali 3 70
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ " regiment company name preTestScore postTestScore\n", "name \n", "Miller Nighthawks 1st Miller 4 25\n", "Jacobson Nighthawks 1st Jacobson 24 94\n", "Ali Nighthawks 2nd Ali 31 57\n", "Milner Nighthawks 2nd Milner 2 62\n", "Cooze Dragoons 1st Cooze 3 70\n", "Jacon Dragoons 1st Jacon 4 25\n", "Ryaner Dragoons 2nd Ryaner 24 94\n", "Sone Dragoons 2nd Sone 31 57\n", "Sloan Scouts 1st Sloan 2 62\n", "Piger Scouts 1st Piger 3 70\n", "Riani Scouts 2nd Riani 2 62\n", "Ali Scouts 2nd Ali 3 70" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "# Transpose the dataset, so that the index (in this case the names) are columns\n", "df.transpose()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameMillerJacobsonAliMilnerCoozeJaconRyanerSoneSloanPigerRianiAli
regiment Nighthawks Nighthawks Nighthawks Nighthawks Dragoons Dragoons Dragoons Dragoons Scouts Scouts Scouts Scouts
company 1st 1st 2nd 2nd 1st 1st 2nd 2nd 1st 1st 2nd 2nd
name Miller Jacobson Ali Milner Cooze Jacon Ryaner Sone Sloan Piger Riani Ali
preTestScore 4 24 31 2 3 4 24 31 2 3 2 3
postTestScore 25 94 57 62 70 25 94 57 62 70 62 70
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "name Miller Jacobson Ali Milner Cooze \\\n", "regiment Nighthawks Nighthawks Nighthawks Nighthawks Dragoons \n", "company 1st 1st 2nd 2nd 1st \n", "name Miller Jacobson Ali Milner Cooze \n", "preTestScore 4 24 31 2 3 \n", "postTestScore 25 94 57 62 70 \n", "\n", "name Jacon Ryaner Sone Sloan Piger Riani Ali \n", "regiment Dragoons Dragoons Dragoons Scouts Scouts Scouts Scouts \n", "company 1st 2nd 2nd 1st 1st 2nd 2nd \n", "name Jacon Ryaner Sone Sloan Piger Riani Ali \n", "preTestScore 4 24 31 2 3 2 3 \n", "postTestScore 25 94 57 62 70 62 70 " ] } ], "prompt_number": 15 } ], "metadata": {} } ] }