Skip to content

Instantly share code, notes, and snippets.

@aborruso
Last active September 22, 2022 07:46
Show Gist options
  • Save aborruso/0389f863caba87e9b45af2016d70c3f4 to your computer and use it in GitHub Desktop.
Save aborruso/0389f863caba87e9b45af2016d70c3f4 to your computer and use it in GitHub Desktop.
Creating non-numeric pivot tables with Python Pandas https://goo.gl/DRzEpB
id key value
id1 size 55
id1 material wood
id2 size 100
id2 material iron
id2 shape round
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from pandas import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import the CSV"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>key</th>\n",
" <th>value</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>id1</td>\n",
" <td>size</td>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>id1</td>\n",
" <td>material</td>\n",
" <td>wood</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>id2</td>\n",
" <td>size</td>\n",
" <td>100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>id2</td>\n",
" <td>material</td>\n",
" <td>iron</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>id2</td>\n",
" <td>shape</td>\n",
" <td>round</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id key value\n",
"0 id1 size 55\n",
"1 id1 material wood\n",
"2 id2 size 100\n",
"3 id2 material iron\n",
"4 id2 shape round"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataFrame=read_csv('enri.csv')\n",
"dataFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set a new index based on two columns"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataFrame.set_index(['id','key'],inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>value</th>\n",
" </tr>\n",
" <tr>\n",
" <th>id</th>\n",
" <th>key</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">id1</th>\n",
" <th>size</th>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>material</th>\n",
" <td>wood</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">id2</th>\n",
" <th>size</th>\n",
" <td>100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>material</th>\n",
" <td>iron</td>\n",
" </tr>\n",
" <tr>\n",
" <th>shape</th>\n",
" <td>round</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" value\n",
"id key \n",
"id1 size 55\n",
" material wood\n",
"id2 size 100\n",
" material iron\n",
" shape round"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run the unstack function\n",
"In this way the values of `key`column are transposed"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>id</th>\n",
" <th colspan=\"3\" halign=\"left\">value</th>\n",
" </tr>\n",
" <tr>\n",
" <th>key</th>\n",
" <th></th>\n",
" <th>material</th>\n",
" <th>shape</th>\n",
" <th>size</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>id1</td>\n",
" <td>wood</td>\n",
" <td>None</td>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>id2</td>\n",
" <td>iron</td>\n",
" <td>round</td>\n",
" <td>100</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id value \n",
"key material shape size\n",
"0 id1 wood None 55\n",
"1 id2 iron round 100"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df=dataFrame.unstack()\n",
"df.reset_index(inplace=True)\n",
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2 (SageMath)",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment