Created
December 20, 2016 13:44
-
-
Save alexpearce/8c187b3ed72c5b1043a2b29282b08816 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": 28, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import itertools\n", | |
"import pandas as pd\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"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>a</th>\n", | |
" <th>b</th>\n", | |
" <th>evt</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>10</td>\n", | |
" <td>100</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>11</td>\n", | |
" <td>101</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>12</td>\n", | |
" <td>102</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>13</td>\n", | |
" <td>103</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>14</td>\n", | |
" <td>104</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>15</td>\n", | |
" <td>105</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>16</td>\n", | |
" <td>106</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>17</td>\n", | |
" <td>107</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>18</td>\n", | |
" <td>108</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>19</td>\n", | |
" <td>109</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" a b evt\n", | |
"0 10 100 1\n", | |
"1 11 101 1\n", | |
"2 12 102 1\n", | |
"3 13 103 1\n", | |
"4 14 104 1\n", | |
"5 15 105 2\n", | |
"6 16 106 2\n", | |
"7 17 107 2\n", | |
"8 18 108 2\n", | |
"9 19 109 2" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df = pd.DataFrame(dict(a= range(10, 20), b=range(100, 110), evt=[1]*5 + [2]*5))\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def compute_num(track1, track2):\n", | |
" return track1.a*track2.a + track1.b*track2.b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"series = {}\n", | |
"for evt, group in grouped:\n", | |
" index = pd.MultiIndex.from_product([group.index, group.index], names=['track1', 'track2'])\n", | |
" evt_nums = []\n", | |
" for idx1, idx2 in index:\n", | |
" evt_nums.append(compute_num(df.loc[idx1], df.loc[idx2]))\n", | |
" series[evt] = pd.Series(evt_nums, index=index)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{1: track1 track2\n", | |
" 0 0 10100\n", | |
" 1 10210\n", | |
" 2 10320\n", | |
" 3 10430\n", | |
" 4 10540\n", | |
" 1 0 10210\n", | |
" 1 10322\n", | |
" 2 10434\n", | |
" 3 10546\n", | |
" 4 10658\n", | |
" 2 0 10320\n", | |
" 1 10434\n", | |
" 2 10548\n", | |
" 3 10662\n", | |
" 4 10776\n", | |
" 3 0 10430\n", | |
" 1 10546\n", | |
" 2 10662\n", | |
" 3 10778\n", | |
" 4 10894\n", | |
" 4 0 10540\n", | |
" 1 10658\n", | |
" 2 10776\n", | |
" 3 10894\n", | |
" 4 11012\n", | |
" dtype: int64, 2: track1 track2\n", | |
" 5 5 11250\n", | |
" 6 11370\n", | |
" 7 11490\n", | |
" 8 11610\n", | |
" 9 11730\n", | |
" 6 5 11370\n", | |
" 6 11492\n", | |
" 7 11614\n", | |
" 8 11736\n", | |
" 9 11858\n", | |
" 7 5 11490\n", | |
" 6 11614\n", | |
" 7 11738\n", | |
" 8 11862\n", | |
" 9 11986\n", | |
" 8 5 11610\n", | |
" 6 11736\n", | |
" 7 11862\n", | |
" 8 11988\n", | |
" 9 12114\n", | |
" 9 5 11730\n", | |
" 6 11858\n", | |
" 7 11986\n", | |
" 8 12114\n", | |
" 9 12242\n", | |
" dtype: int64}" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Maybe you can combine this dictionary into one big DataFrame/Series with a new MultiIndex?\n", | |
"series" | |
] | |
}, | |
{ | |
"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": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment