Created
January 26, 2017 20:40
-
-
Save baldwint/b1fd56ad9f7653fdf5d451b81bc45b98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Inserting index levels" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def insert_index_level(index, val, level=None, name=None):\n", | |
| " \"\"\"Insert a new level to a MultiIndex. Will be called `val` for all data.\n", | |
| " \n", | |
| " Optionally specify which `level`the new level will be, otherwise it will be innermost.\n", | |
| " \n", | |
| " \"\"\" \n", | |
| " if index.nlevels > 1: # already a multiindex\n", | |
| " tuples = index.values\n", | |
| " else: # only a single index\n", | |
| " tuples = ((v,) for v in index.values)\n", | |
| " if level is None:\n", | |
| " level = index.nlevels\n", | |
| " new_names = list(index.names)\n", | |
| " new_names.insert(level, name)\n", | |
| " new_labels = [(tup[:level] + (val,) + tup[level:]) for tup in tuples]\n", | |
| " return pd.MultiIndex.from_tuples(new_labels, names=new_names)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "example usage:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "np.random.seed(0)\n", | |
| "data = np.random.randn(4,4)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "idx = pd.MultiIndex.from_tuples([(1, u'one'), (1, u'two'),\n", | |
| " (2, u'one'), (2, u'two')],\n", | |
| " names=['foo', 'bar'])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>foo</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">1</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">2</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>bar</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "foo 1 2 \n", | |
| "bar one two one two\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "test = pd.DataFrame(data=data, columns=idx)\n", | |
| "test" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>foo</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">1</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">2</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>new</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">hi</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">hi</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>bar</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "foo 1 2 \n", | |
| "new hi hi \n", | |
| "bar one two one two\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "test.columns = insert_index_level(test.columns, 'hi', level=1, name='new')\n", | |
| "test" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "this will turn Index to MultiIndex if necessary:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "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>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>three</th>\n", | |
| " <th>four</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " one two three four\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "single = pd.DataFrame(data=data, columns=('one', 'two', 'three', 'four'))\n", | |
| "single" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>new</th>\n", | |
| " <th colspan=\"4\" halign=\"left\">hi</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th></th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>three</th>\n", | |
| " <th>four</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "new hi \n", | |
| " one two three four\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "single.columns = insert_index_level(single.columns, 'hi', level=0, name='new')\n", | |
| "single" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "bak = single.columns" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# What's the point\n", | |
| "\n", | |
| "Sometimes I want to concatenate two frames whose indices do not have the same `nlevels`. When I do this, the index becomes unreadable because not all the tuples have the same length." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "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>(1, hi, one)</th>\n", | |
| " <th>(1, hi, two)</th>\n", | |
| " <th>(2, hi, one)</th>\n", | |
| " <th>(2, hi, two)</th>\n", | |
| " <th>(hi, one)</th>\n", | |
| " <th>(hi, two)</th>\n", | |
| " <th>(hi, three)</th>\n", | |
| " <th>(hi, four)</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " (1, hi, one) (1, hi, two) (2, hi, one) (2, hi, two) (hi, one) \\\n", | |
| "0 1.764052 0.400157 0.978738 2.240893 1.764052 \n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357 1.867558 \n", | |
| "2 -0.103219 0.410599 0.144044 1.454274 -0.103219 \n", | |
| "3 0.761038 0.121675 0.443863 0.333674 0.761038 \n", | |
| "\n", | |
| " (hi, two) (hi, three) (hi, four) \n", | |
| "0 0.400157 0.978738 2.240893 \n", | |
| "1 -0.977278 0.950088 -0.151357 \n", | |
| "2 0.410599 0.144044 1.454274 \n", | |
| "3 0.121675 0.443863 0.333674 " | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "pd.concat((test, single), axis=1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "now I can resolve this by inserting a level to the shallow multiindex:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>foo</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">1</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">2</th>\n", | |
| " <th colspan=\"4\" halign=\"left\">3</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>new</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">hi</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">hi</th>\n", | |
| " <th colspan=\"4\" halign=\"left\">hi</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>bar</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>three</th>\n", | |
| " <th>four</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "foo 1 2 3 \\\n", | |
| "new hi hi hi \n", | |
| "bar one two one two one two three \n", | |
| "0 1.764052 0.400157 0.978738 2.240893 1.764052 0.400157 0.978738 \n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357 1.867558 -0.977278 0.950088 \n", | |
| "2 -0.103219 0.410599 0.144044 1.454274 -0.103219 0.410599 0.144044 \n", | |
| "3 0.761038 0.121675 0.443863 0.333674 0.761038 0.121675 0.443863 \n", | |
| "\n", | |
| "foo \n", | |
| "new \n", | |
| "bar four \n", | |
| "0 2.240893 \n", | |
| "1 -0.151357 \n", | |
| "2 1.454274 \n", | |
| "3 0.333674 " | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "single.columns = insert_index_level(single.columns, 3, 0)\n", | |
| "pd.concat((test, single), axis=1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "this is best, but occasionaly it is nicer to reduce the number of levels in the deep one:\n", | |
| "\n", | |
| "# combining index levels" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def flatten_levels(index, lo=0, hi=None, delimiter='_'):\n", | |
| " \"\"\"reduce number of levels in a MultiIndex by string concatenation\"\"\"\n", | |
| " assert index.nlevels > 1\n", | |
| " if hi is None:\n", | |
| " hi = index.nlevels\n", | |
| " def collapse(tup):\n", | |
| " return (tup[:lo] + (delimiter.join(el for el in tup[lo:hi] if el is not None),) + tup[hi:])\n", | |
| " new_values = [collapse(tup) for tup in index.values]\n", | |
| " new_names = collapse(index.names)\n", | |
| " return pd.MultiIndex.from_tuples(new_values, names=new_names)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>new</th>\n", | |
| " <th colspan=\"4\" halign=\"left\">hi</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th></th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>three</th>\n", | |
| " <th>four</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "new hi \n", | |
| " one two three four\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "single.columns = bak\n", | |
| "single" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>foo</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">1</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">2</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>new_bar</th>\n", | |
| " <th>hi_one</th>\n", | |
| " <th>hi_two</th>\n", | |
| " <th>hi_one</th>\n", | |
| " <th>hi_two</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "foo 1 2 \n", | |
| "new_bar hi_one hi_two hi_one hi_two\n", | |
| "0 1.764052 0.400157 0.978738 2.240893\n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357\n", | |
| "2 -0.103219 0.410599 0.144044 1.454274\n", | |
| "3 0.761038 0.121675 0.443863 0.333674" | |
| ] | |
| }, | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "test.columns = flatten_levels(test.columns, lo=1)\n", | |
| "test" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr>\n", | |
| " <th>new</th>\n", | |
| " <th colspan=\"4\" halign=\"left\">hi</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">1</th>\n", | |
| " <th colspan=\"2\" halign=\"left\">2</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th></th>\n", | |
| " <th>one</th>\n", | |
| " <th>two</th>\n", | |
| " <th>three</th>\n", | |
| " <th>four</th>\n", | |
| " <th>hi_one</th>\n", | |
| " <th>hi_two</th>\n", | |
| " <th>hi_one</th>\n", | |
| " <th>hi_two</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " <td>1.764052</td>\n", | |
| " <td>0.400157</td>\n", | |
| " <td>0.978738</td>\n", | |
| " <td>2.240893</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " <td>1.867558</td>\n", | |
| " <td>-0.977278</td>\n", | |
| " <td>0.950088</td>\n", | |
| " <td>-0.151357</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " <td>-0.103219</td>\n", | |
| " <td>0.410599</td>\n", | |
| " <td>0.144044</td>\n", | |
| " <td>1.454274</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " <td>0.761038</td>\n", | |
| " <td>0.121675</td>\n", | |
| " <td>0.443863</td>\n", | |
| " <td>0.333674</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| "new hi 1 2 \\\n", | |
| " one two three four hi_one hi_two hi_one \n", | |
| "0 1.764052 0.400157 0.978738 2.240893 1.764052 0.400157 0.978738 \n", | |
| "1 1.867558 -0.977278 0.950088 -0.151357 1.867558 -0.977278 0.950088 \n", | |
| "2 -0.103219 0.410599 0.144044 1.454274 -0.103219 0.410599 0.144044 \n", | |
| "3 0.761038 0.121675 0.443863 0.333674 0.761038 0.121675 0.443863 \n", | |
| "\n", | |
| "new \n", | |
| " hi_two \n", | |
| "0 2.240893 \n", | |
| "1 -0.151357 \n", | |
| "2 1.454274 \n", | |
| "3 0.333674 " | |
| ] | |
| }, | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "pd.concat([single, test], axis=1)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment