Last active
December 7, 2016 01:29
-
-
Save yuu-ito/721bb6400739b6666b71a26973ce3a57 to your computer and use it in GitHub Desktop.
Python pandas.DataFrame.to_dict
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": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "- http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "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>_mu</th>\n", | |
| " <th>_sigma</th>\n", | |
| " <th>normal</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1000</td>\n", | |
| " <td>100</td>\n", | |
| " <td>886.616167</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>1000</td>\n", | |
| " <td>100</td>\n", | |
| " <td>1038.431919</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>1000</td>\n", | |
| " <td>100</td>\n", | |
| " <td>1149.655378</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>1000</td>\n", | |
| " <td>100</td>\n", | |
| " <td>964.461770</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>1000</td>\n", | |
| " <td>100</td>\n", | |
| " <td>921.246646</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " _mu _sigma normal\n", | |
| "0 1000 100 886.616167\n", | |
| "1 1000 100 1038.431919\n", | |
| "2 1000 100 1149.655378\n", | |
| "3 1000 100 964.461770\n", | |
| "4 1000 100 921.246646" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "np.random.seed(111)\n", | |
| "mu, sigma, size = 1000, 100, 5\n", | |
| "x = pd.DataFrame({\n", | |
| " '_mu':mu,\n", | |
| " '_sigma':sigma,\n", | |
| " 'normal':np.random.normal(mu,sigma,size)\n", | |
| " })\n", | |
| "x" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'_mu': {0: 1000, 1: 1000, 2: 1000, 3: 1000, 4: 1000},\n", | |
| " '_sigma': {0: 100, 1: 100, 2: 100, 3: 100, 4: 100},\n", | |
| " 'normal': {0: 886.61616656909678,\n", | |
| " 1: 1038.4319193399647,\n", | |
| " 2: 1149.6553776370522,\n", | |
| " 3: 964.46177028375462,\n", | |
| " 4: 921.24664590713189}}" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"dict\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'_mu': [1000, 1000, 1000, 1000, 1000],\n", | |
| " '_sigma': [100, 100, 100, 100, 100],\n", | |
| " 'normal': [886.6161665690968,\n", | |
| " 1038.4319193399647,\n", | |
| " 1149.6553776370522,\n", | |
| " 964.4617702837546,\n", | |
| " 921.2466459071319]}" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"list\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'_mu': 0 1000\n", | |
| " 1 1000\n", | |
| " 2 1000\n", | |
| " 3 1000\n", | |
| " 4 1000\n", | |
| " Name: _mu, dtype: int64, '_sigma': 0 100\n", | |
| " 1 100\n", | |
| " 2 100\n", | |
| " 3 100\n", | |
| " 4 100\n", | |
| " Name: _sigma, dtype: int64, 'normal': 0 886.616167\n", | |
| " 1 1038.431919\n", | |
| " 2 1149.655378\n", | |
| " 3 964.461770\n", | |
| " 4 921.246646\n", | |
| " Name: normal, dtype: float64}" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"series\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'columns': ['_mu', '_sigma', 'normal'],\n", | |
| " 'data': [[1000.0, 100.0, 886.6161665690968],\n", | |
| " [1000.0, 100.0, 1038.4319193399647],\n", | |
| " [1000.0, 100.0, 1149.6553776370522],\n", | |
| " [1000.0, 100.0, 964.4617702837546],\n", | |
| " [1000.0, 100.0, 921.2466459071319]],\n", | |
| " 'index': [0, 1, 2, 3, 4]}" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"split\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[{'_mu': 1000.0, '_sigma': 100.0, 'normal': 886.61616656909678},\n", | |
| " {'_mu': 1000.0, '_sigma': 100.0, 'normal': 1038.4319193399647},\n", | |
| " {'_mu': 1000.0, '_sigma': 100.0, 'normal': 1149.6553776370522},\n", | |
| " {'_mu': 1000.0, '_sigma': 100.0, 'normal': 964.46177028375462},\n", | |
| " {'_mu': 1000.0, '_sigma': 100.0, 'normal': 921.24664590713189}]" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"records\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{0: {'_mu': 1000.0, '_sigma': 100.0, 'normal': 886.61616656909678},\n", | |
| " 1: {'_mu': 1000.0, '_sigma': 100.0, 'normal': 1038.4319193399647},\n", | |
| " 2: {'_mu': 1000.0, '_sigma': 100.0, 'normal': 1149.6553776370522},\n", | |
| " 3: {'_mu': 1000.0, '_sigma': 100.0, 'normal': 964.46177028375462},\n", | |
| " 4: {'_mu': 1000.0, '_sigma': 100.0, 'normal': 921.24664590713189}}" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "x.to_dict(orient=\"index\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "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": 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment