Created
October 12, 2014 04:29
-
-
Save satouy/6583b0993acf7be90388 to your computer and use it in GitHub Desktop.
Take-r-pandas-dataframe
This file contains 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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:fdcdfed4c6430cca0d911e425595a5d8b5cfc60a945170170733883e029ab847" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"R\u3068Pandas(Sage)\u3067\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u76f8\u4e92\u5909\u63db" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%pylab inline" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Populating the interactive namespace from numpy and matplotlib\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"\u30c7\u30fc\u30bf\u5909\u63db\u3067\u4f7f\u7528\u3059\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u3068\u30e9\u30a4\u30d6\u30e9\u30ea" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# R\u3068Pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u76f8\u4e92\u306b\u5909\u63db\u3059\u308b\u65b9\u6cd5\n", | |
"# Sage\u3067\u306f\u3001numpy\u3068pandas\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\n", | |
"import pandas as pd\n", | |
"import numpy as np" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import rpy2.robjects as robjects\n", | |
"ro = robjects.r" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# \u4f8b\u3068\u3057\u3066R Graphic Cookbook\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\n", | |
"#ro(\"install.packages('gcookbook')\")\n", | |
"ro('library(gcookbook)')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"<StrVector - Python:0x1131c47e8 / R:0x7fe1fe23eff8>\n", | |
"['gcoo..., 'tools', 'stats', ..., 'data..., 'meth..., 'base']" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# R\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u5909\u63db\u3059\u308b\n", | |
"import pandas.rpy.common as com\n", | |
"\n", | |
"# heightweight = com.load_data('heightweight') \n", | |
"# R\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u5909\u63db\u3059\u308b\n", | |
"# r_dataframe = com.convert_to_r_dataframe(df)\n", | |
"# pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092R\u306b\u6e21\u3059" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from sage.all import *" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"R\u304b\u3089Pandas\u3078\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u5909\u63db " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# print RDf2PandaDf('heightweight').head()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"heightweight = com.load_data('heightweight')\n", | |
"heightweight.head()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>sex</th>\n", | |
" <th>ageYear</th>\n", | |
" <th>ageMonth</th>\n", | |
" <th>heightIn</th>\n", | |
" <th>weightLb</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td> f</td>\n", | |
" <td> 11.92</td>\n", | |
" <td> 143</td>\n", | |
" <td> 56.3</td>\n", | |
" <td> 85.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td> f</td>\n", | |
" <td> 12.92</td>\n", | |
" <td> 155</td>\n", | |
" <td> 62.3</td>\n", | |
" <td> 105.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td> f</td>\n", | |
" <td> 12.75</td>\n", | |
" <td> 153</td>\n", | |
" <td> 63.3</td>\n", | |
" <td> 108.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td> f</td>\n", | |
" <td> 13.42</td>\n", | |
" <td> 161</td>\n", | |
" <td> 59.0</td>\n", | |
" <td> 92.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td> f</td>\n", | |
" <td> 15.92</td>\n", | |
" <td> 191</td>\n", | |
" <td> 62.5</td>\n", | |
" <td> 112.5</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
" sex ageYear ageMonth heightIn weightLb\n", | |
"1 f 11.92 143 56.3 85.0\n", | |
"2 f 12.92 155 62.3 105.0\n", | |
"3 f 12.75 153 63.3 108.0\n", | |
"4 f 13.42 161 59.0 92.0\n", | |
"5 f 15.92 191 62.5 112.5" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"Pandas\u304b\u3089R\u3078\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u5909\u63db " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Pandas\u306e\u30c7\u30fc\u30bf\u3092R\u306b\u6e21\u3059\n", | |
"age = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]\n", | |
"sex = ['F', 'M', 'M', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'F', 'M']\n", | |
"df = pd.DataFrame({'age': age, 'sex': sex}); df.head()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>age</th>\n", | |
" <th>sex</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td> 20</td>\n", | |
" <td> F</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td> 22</td>\n", | |
" <td> M</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td> 25</td>\n", | |
" <td> M</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td> 27</td>\n", | |
" <td> M</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td> 21</td>\n", | |
" <td> F</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": [ | |
" age sex\n", | |
"0 20 F\n", | |
"1 22 M\n", | |
"2 25 M\n", | |
"3 27 M\n", | |
"4 21 F" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# PandaDf2RDf(df, \"a\")\n", | |
"# r('a')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"r_dataframe = com.convert_to_r_dataframe(df)\n", | |
"print(type(r_dataframe))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<class 'rpy2.robjects.vectors.DataFrame'>\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print(r_dataframe)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" age sex\n", | |
"0 20 F\n", | |
"1 22 M\n", | |
"2 25 M\n", | |
"3 27 M\n", | |
"4 21 F\n", | |
"5 23 M\n", | |
"6 37 F\n", | |
"7 31 M\n", | |
"8 61 F\n", | |
"9 45 M\n", | |
"10 41 F\n", | |
"11 32 M\n", | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment