Skip to content

Instantly share code, notes, and snippets.

@kozo2
Last active August 2, 2016 18:30
Show Gist options
  • Save kozo2/079b96b8ebbf270a006d87f0c3d48884 to your computer and use it in GitHub Desktop.
Save kozo2/079b96b8ebbf270a006d87f0c3d48884 to your computer and use it in GitHub Desktop.
Pandas cookbook with Daru
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'daru'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Idioms"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70198583207820 rows: 4 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>0</td><td>4</td><td>10</td><td>100</td></tr><tr><td>1</td><td>5</td><td>20</td><td>50</td></tr><tr><td>2</td><td>6</td><td>30</td><td>-30</td></tr><tr><td>3</td><td>7</td><td>40</td><td>-50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70198583207820 @name = 390b6b84-1db2-4590-b25b-bfdc1cddaae8 @size = 4>\n",
" AAA BBB CCC \n",
" 0 4 10 100 \n",
" 1 5 20 50 \n",
" 2 6 30 -30 \n",
" 3 7 40 -50 \n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = Daru::DataFrame.new({\n",
" 'AAA' => [4,5,6,7],\n",
" 'BBB' => [10,20,30,40],\n",
" 'CCC' => [100,50,-30,-50]\n",
" })"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### if-then..."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70198583097000 rows: 3 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>1</td><td>5</td><td>20</td><td>50</td></tr><tr><td>2</td><td>6</td><td>30</td><td>-30</td></tr><tr><td>3</td><td>7</td><td>40</td><td>-50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70198583097000 @name = 67ea87d1-1c6e-4e71-8f23-17adb12f529f @size = 3>\n",
" AAA BBB CCC \n",
" 1 5 20 50 \n",
" 2 6 30 -30 \n",
" 3 7 40 -50 \n"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.where(df['AAA'].gteq(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### splitting"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70198582983880 rows: 4 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>0</td><td>4</td><td>10</td><td>100</td></tr><tr><td>1</td><td>5</td><td>20</td><td>50</td></tr><tr><td>2</td><td>6</td><td>30</td><td>-30</td></tr><tr><td>3</td><td>7</td><td>40</td><td>-50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70198582983880 @name = c6403e8d-c3b1-4593-9a02-979374fcfda2 @size = 4>\n",
" AAA BBB CCC \n",
" 0 4 10 100 \n",
" 1 5 20 50 \n",
" 2 6 30 -30 \n",
" 3 7 40 -50 \n"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = Daru::DataFrame.new({\n",
" 'AAA' => [4,5,6,7],\n",
" 'BBB' => [10,20,30,40],\n",
" 'CCC' => [100,50,-30,-50]\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70198582912800 rows: 2 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>0</td><td>4</td><td>10</td><td>100</td></tr><tr><td>1</td><td>5</td><td>20</td><td>50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70198582912800 @name = ff6e971b-20a3-438b-9a8e-1920fe7fe218 @size = 2>\n",
" AAA BBB CCC \n",
" 0 4 10 100 \n",
" 1 5 20 50 \n"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#dflow = df[df.AAA <= 5]\n",
"dflow = df.where(df['AAA'].lteq(5))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70198582796960 rows: 2 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>2</td><td>6</td><td>30</td><td>-30</td></tr><tr><td>3</td><td>7</td><td>40</td><td>-50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70198582796960 @name = cfa034b7-fa6d-4f9e-8056-261067d4179b @size = 2>\n",
" AAA BBB CCC \n",
" 2 6 30 -30 \n",
" 3 7 40 -50 \n"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfhigh = df.where(df['AAA'].gt(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Building Criteria"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"4\">Daru::DataFrame:70276282122140 rows: 4 cols: 3</th></tr><tr><th></th><th>AAA</th><th>BBB</th><th>CCC</th></tr><tr><td>0</td><td>4</td><td>10</td><td>100</td></tr><tr><td>1</td><td>5</td><td>20</td><td>50</td></tr><tr><td>2</td><td>6</td><td>30</td><td>-30</td></tr><tr><td>3</td><td>7</td><td>40</td><td>-50</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::DataFrame:70276282122140 @name = a20002ea-4758-4a93-9a1a-6fe6500d4493 @size = 4>\n",
" AAA BBB CCC \n",
" 0 4 10 100 \n",
" 1 5 20 50 \n",
" 2 6 30 -30 \n",
" 3 7 40 -50 \n"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = Daru::DataFrame.new({\n",
" 'AAA' => [4,5,6,7],\n",
" 'BBB' => [10,20,30,40],\n",
" 'CCC' => [100,50,-30,-50]\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"2\">Daru::Vector:70198560516280 size: 2</th></tr><tr><th> </th><th>nil</th></tr><tr><td>0</td><td>4</td></tr><tr><td>1</td><td>5</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::Vector:70198560516280 @name = nil @metadata = {} @size = 2 >\n",
" nil\n",
" 0 4\n",
" 1 5\n"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.where(df['BBB'].lt(25) & df['CCC'].gteq(-40))['AAA']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table><tr><th colspan=\"2\">Daru::Vector:70276290943060 size: 4</th></tr><tr><th> </th><th>nil</th></tr><tr><td>0</td><td>4</td></tr><tr><td>1</td><td>5</td></tr><tr><td>2</td><td>6</td></tr><tr><td>3</td><td>7</td></tr></table>"
],
"text/plain": [
"\n",
"#<Daru::Vector:70276290943060 @name = nil @metadata = {} @size = 4 >\n",
" nil\n",
" 0 4\n",
" 1 5\n",
" 2 6\n",
" 3 7\n"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.where(df['BBB'].gt(25) | df['CCC'].gteq(-40))['AAA']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.3.1",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.3.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment