Skip to content

Instantly share code, notes, and snippets.

@jrovegno
Created December 20, 2015 21:20
Show Gist options
  • Save jrovegno/43107d780ef75c07854d to your computer and use it in GitHub Desktop.
Save jrovegno/43107d780ef75c07854d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'daru'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:24862284 rows: 6 cols: 3<tr><th></th><th>col0</th><th>col1</th><th>col2</th></tr><tr><td>one</td><td>1</td><td>11</td><td>a</td></tr><tr><td>two</td><td>2</td><td>22</td><td>b</td></tr><tr><td>three</td><td>3</td><td>33</td><td>c</td></tr><tr><td>four</td><td>4</td><td>44</td><td>d</td></tr><tr><td>five</td><td>5</td><td>55</td><td>e</td></tr><tr><td>six</td><td>6</td><td>66</td><td>f</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:24862284 @name = 18726c80-d34d-4e60-a6ee-098a5913bfc0 @size = 6>\n",
" col0 col1 col2 \n",
" one 1 11 a \n",
" two 2 22 b \n",
" three 3 33 c \n",
" four 4 44 d \n",
" five 5 55 e \n",
" six 6 66 f \n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = Daru::DataFrame.new({\n",
" 'col0' => [1,2,3,4,5,6],\n",
" 'col2' => ['a','b','c','d','e','f'],\n",
" 'col1' => [11,22,33,44,55,66]\n",
" }, \n",
" index: ['one', 'two', 'three', 'four', 'five', 'six'], \n",
" order: ['col0', 'col1', 'col2']\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"#<Daru::Index:0x2f6b470 @relation_hash={\"one\"=>0, \"two\"=>1, \"three\"=>2, \"four\"=>3, \"five\"=>4, \"six\"=>5}, @keys=[\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"], @size=6>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.index"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df.write_excel('df.xls'); nil"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:27133044 rows: 6 cols: 3<tr><th></th><th>col0</th><th>col1</th><th>col2</th></tr><tr><td>0</td><td>1</td><td>11</td><td>a</td></tr><tr><td>1</td><td>2</td><td>22</td><td>b</td></tr><tr><td>2</td><td>3</td><td>33</td><td>c</td></tr><tr><td>3</td><td>4</td><td>44</td><td>d</td></tr><tr><td>4</td><td>5</td><td>55</td><td>e</td></tr><tr><td>5</td><td>6</td><td>66</td><td>f</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:27133044 @name = d53dd3ef-1180-49a6-9755-91e307330d8f @size = 6>\n",
" col0 col1 col2 \n",
" 0 1 11 a \n",
" 1 2 22 b \n",
" 2 3 33 c \n",
" 3 4 44 d \n",
" 4 5 55 e \n",
" 5 6 66 f \n"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1 = Daru::DataFrame.from_excel('df.xls')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"]\n",
"[0, 1, 2, 3, 4, 5]"
]
}
],
"source": [
"# Index [\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"] not stored in excel\n",
"print df.index.to_a\n",
"print \"\\n\"\n",
"print df1.index.to_a"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\"col0\", \"col1\", \"col2\"]\n",
"[:col0, :col1, :col2]"
]
}
],
"source": [
"print df.row[0].index.to_a\n",
"print \"\\n\"\n",
"print df1.row[0].index.to_a"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"#<Daru::Index:0x2f6b470 @relation_hash={\"one\"=>0, \"two\"=>1, \"three\"=>2, \"four\"=>3, \"five\"=>4, \"six\"=>5}, @keys=[\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"], @size=6>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.index"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"#<Daru::Index:0x33b7198 @relation_hash={0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5}, @keys=[0, 1, 2, 3, 4, 5], @size=6>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1.index"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"2\">Daru::Vector:24860784 size: 6</th></tr><tr><th> </th><th>col0</th></tr><tr><td>one</td><td>1</td></tr><tr><td>two</td><td>2</td></tr><tr><td>three</td><td>3</td></tr><tr><td>four</td><td>4</td></tr><tr><td>five</td><td>5</td></tr><tr><td>six</td><td>6</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::Vector:24860784 @name = col0 @size = 6 >\n",
" col0\n",
" one 1\n",
" two 2\n",
"three 3\n",
" four 4\n",
" five 5\n",
" six 6\n"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['col0']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"2\">Daru::Vector:27113736 size: 6</th></tr><tr><th> </th><th>nil</th></tr><tr><td>0</td><td>1</td></tr><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>3</td></tr><tr><td>3</td><td>4</td></tr><tr><td>4</td><td>5</td></tr><tr><td>5</td><td>6</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::Vector:27113736 @name = nil @size = 6 >\n",
" nil\n",
" 0 1\n",
" 1 2\n",
" 2 3\n",
" 3 4\n",
" 4 5\n",
" 5 6\n"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1[:col0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.7",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment