Skip to content

Instantly share code, notes, and snippets.

@ColinTalbert
Last active August 16, 2018 13:56
Show Gist options
  • Save ColinTalbert/0edc8c9d8d26db72ae5d8d10962d430c to your computer and use it in GitHub Desktop.
Save ColinTalbert/0edc8c9d8d26db72ae5d8d10962d430c 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": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>scenario</th>\n",
" <th>variable</th>\n",
" <th>P10</th>\n",
" <th>P90</th>\n",
" <th>median</th>\n",
" <th>combSum</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>No action</td>\n",
" <td>Growth</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>nan (nan - nan)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>No action</td>\n",
" <td>OffRange</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0 (0.0 - 0.0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>No action</td>\n",
" <td>OnRange</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0 (0.0 - 0.0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>No action</td>\n",
" <td>TotPop</td>\n",
" <td>300.0</td>\n",
" <td>300.0</td>\n",
" <td>300.0</td>\n",
" <td>300.0 (300.0 - 300.0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>No action</td>\n",
" <td>TotalCost</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0 (0.0 - 0.0)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" year scenario variable P10 P90 median combSum\n",
"0 0 No action Growth NaN NaN NaN nan (nan - nan)\n",
"1 0 No action OffRange 0.0 0.0 0.0 0.0 (0.0 - 0.0)\n",
"2 0 No action OnRange 0.0 0.0 0.0 0.0 (0.0 - 0.0)\n",
"3 0 No action TotPop 300.0 300.0 300.0 300.0 (300.0 - 300.0)\n",
"4 0 No action TotalCost 0.0 0.0 0.0 0.0 (0.0 - 0.0)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fname = r\"C:\\Users\\talbertc\\Downloads\\outputDF2.csv\"\n",
"\n",
"df = pd.read_csv(fname)\n",
"df.head(5)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def str_num(num):\n",
" s = str(num)\n",
" if s.endswith('.0'):\n",
" s = s[:-2]\n",
" return s\n",
"\n",
"def format_combsum(row):\n",
" return f\"{str_num(row['P10'])} ({str_num(row['P90'])} - {str_num(row['median'])})\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"df['combSum'] = df.apply(format_combsum, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>scenario</th>\n",
" <th>variable</th>\n",
" <th>P10</th>\n",
" <th>P90</th>\n",
" <th>median</th>\n",
" <th>combSum</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>0</td>\n",
" <td>Spay only</td>\n",
" <td>Growth</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>nan (nan - nan)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>0</td>\n",
" <td>Spay only</td>\n",
" <td>OffRange</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>0</td>\n",
" <td>Spay only</td>\n",
" <td>OnRange</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>0</td>\n",
" <td>Spay only</td>\n",
" <td>TotPop</td>\n",
" <td>300.00</td>\n",
" <td>300.00</td>\n",
" <td>300.00</td>\n",
" <td>300 (300 - 300)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>0</td>\n",
" <td>Spay only</td>\n",
" <td>TotalCost</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>1</td>\n",
" <td>No action</td>\n",
" <td>Growth</td>\n",
" <td>0.98</td>\n",
" <td>1.15</td>\n",
" <td>1.08</td>\n",
" <td>0.98 (1.15 - 1.08)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>1</td>\n",
" <td>No action</td>\n",
" <td>OffRange</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>1</td>\n",
" <td>No action</td>\n",
" <td>OnRange</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>1</td>\n",
" <td>No action</td>\n",
" <td>TotPop</td>\n",
" <td>294.00</td>\n",
" <td>346.00</td>\n",
" <td>322.00</td>\n",
" <td>294 (346 - 322)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>1</td>\n",
" <td>No action</td>\n",
" <td>TotalCost</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0.00</td>\n",
" <td>0 (0 - 0)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>1</td>\n",
" <td>Removals only</td>\n",
" <td>Growth</td>\n",
" <td>0.97</td>\n",
" <td>1.15</td>\n",
" <td>1.09</td>\n",
" <td>0.97 (1.15 - 1.09)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" year scenario variable P10 P90 median combSum\n",
"10 0 Spay only Growth NaN NaN NaN nan (nan - nan)\n",
"11 0 Spay only OffRange 0.00 0.00 0.00 0 (0 - 0)\n",
"12 0 Spay only OnRange 0.00 0.00 0.00 0 (0 - 0)\n",
"13 0 Spay only TotPop 300.00 300.00 300.00 300 (300 - 300)\n",
"14 0 Spay only TotalCost 0.00 0.00 0.00 0 (0 - 0)\n",
"15 1 No action Growth 0.98 1.15 1.08 0.98 (1.15 - 1.08)\n",
"16 1 No action OffRange 0.00 0.00 0.00 0 (0 - 0)\n",
"17 1 No action OnRange 0.00 0.00 0.00 0 (0 - 0)\n",
"18 1 No action TotPop 294.00 346.00 322.00 294 (346 - 322)\n",
"19 1 No action TotalCost 0.00 0.00 0.00 0 (0 - 0)\n",
"20 1 Removals only Growth 0.97 1.15 1.09 0.97 (1.15 - 1.09)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[10:20]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment